当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Controller dispatch error in Zend framework 2

问题描述:

I am returning Json response from zend controller.

This is my code

Controller Code

public function get()

{

$result = //calling other function and getting response in array

print("value in controller before calling toJsonModel");

print_r($result);

$temp = $this->toJsonModel($result);

var_dump($temp);

return $temp;

}

toJsonModel function

public function toJsonModel($data = array())

{

$data['requestId'] = $this->getHeader('RequestId');

print("Value just before returning to controller");

print_r($data);

return new JsonModel($data);

}

Response

First and second print displays correct values. But after getting values from toJsonModel when I try to displays wrong values and adds some other values like "captureTo", "terminate" and "children" as protected.

I don't know why it's giving me wrong message.

Can any one guide me to right path and tell me what the problem is ?

Thanks in advance.

EDIT

I am adding new screenshot to display full message.

ERROR :

{"name":"Email Not Found","message":"No account found with this email

address.","code":404,"requestId":"12","reason":"error-controller-cannot-dispatch","display_exceptions":true,"controller":"RSMobile\Controller\ForgotPassword","controller_class":null}

网友答案:

This is because you are returning an object of JsonModel from method toJsonModel.

return new JsonModel($data);

So, every object has some properties that you can use to manipulate data. See the docs of Zend\View\Model\JsonModel here

So actually the values are not wrong but there is a way to access properties of an object.

Update

Attaching dispatch event

public function onBootstrap($e)
{
    // some stuff
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach("dispatch", function($e) {
        echo "Dispatch!";
    });
    // some stuff
}
网友答案:

I finally solved this problem.

I was throwing some error from controller whenever there is a problem. I used to throw 404 when email not found. That was causing this error.

At first i thought that this is a problem related to JSON model. But thanks to @Bilal who helped me in figuring out the problem.

When I throw 404 from controller it by default appends all these data at the end. But when i throw any error other than 404, it works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: