문제

I want to return status code 403 from Module.php whenever authorization fails. I searched and found out that I need to attach listener to this file.

I am trying to do it this way. But I am getting 200 even if I am setting statusCode to 500. See the code below.

    $em = $e->getApplication()->getEventManager();
    // Authorize API call
    $auth =  $this->authorize($e->getApplication()->getServiceManager());

    if(!auth)
    {
            $app = $e->getApplication();

            $em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($app) {

                $response = $e->getResponse();
                $response->setStatusCode(500);

                $response->setContent(json_encode(array(
                             'message' => 'You are not authorized for this request',
                 )));

                 return $response;

           }, PHP_INT_MAX);


    }

Response from API Call

도움이 되었습니까?

해결책

Some other controller was causing problem and when I copied and paste some of the code, It started to work..

Thanks @ins0 for this suggestion.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top