I am building REST API on Zend Framework 2. I want to send certain status code in response whenever any error has occurred.
I tried below in my controller :
$statusCode = 401;
$this->response->setStatusCode($statusCode);
return new JsonModel(array("error message" => "error description"));
Echoing status code prints 401
, but client-side application gets status code 200
every time.
How can I set status code to particular value?
Module.php :
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
EDIT : Below is how the response looks :
HTTP/1.0 401 Unauthorized
. – PadrongetResponse()
, too. – TrichinizemyController
, which extendsAbstractRestfulController
. All controllers then extendmyController
. – Trichinizeget()
, which is inherited fromAbstractRESTfulController
. – Trichinize