0

我想在每次授權失敗時從Module.php返回狀態代碼403。我搜索並發現我需要將偵聽器附加到此文件。始終從Module.php返回狀態代碼200

我想這樣做。但即使我將statusCode設置爲500,我也會得到200. 請參閱下面的代碼。

$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

+0

你是否在你的'onBootstrap'方法中有這樣的代碼,你確定這個模塊在你的application.config.php中被啓用了嗎? –

+0

是這段代碼是在onBootstrap方法中。我想如果它被禁用了,那麼它也不會給這個消息。 – keen

+1

是上一個穩定版本中的zf2版本嗎?上面的代碼在我的應用程序中給了我預期的結果。或者其他一些用戶組件搞亂了響應代碼? – ins0

回答

0

一些其他的控制器是造成問題,當我複製並粘貼一些代碼,它開始工作..

感謝@ ins0這一建議。

相關問題