2016-11-07 23 views
1

我對PHP相對來說比較新,這可能很好地證明是這裏的主要問題 - 我感覺我錯過了一些對PHP內部感到基本的東西,直接解決這個問題,或讓它顯而易見地爲什麼我浪費我的時間!...與其他中間件一起使用entomb/slim-json-api

基本上在下面的Slim API代碼中,我希望由優秀的entomb/slim-json-api中間件添加的異常處理也適用於後續的myMiddleware。如下實施,似乎只來處理途徑編碼產生故障...

(PHP v 5.4.17)

$ php composer.phar info 
entomb/slim-json-api dev-master c11e001 Slim extension to implement fast JSON API's 
slim/slim   2.6.2    Slim Framework, a PHP micro framework 

API代碼:

require 'vendor/autoload.php'; 
use Slim\Middleware; 

class myMiddleware extends Middleware 
{ 
    public function call() 
    { 
     $uri_array = explode('/', $this->app->request->getResourceUri()); 
     $env = $this->app->environment; 

     if($uri_array[2] == 98) { 
      throw new \Exception("User $uri_array[2], you are not even welcome in middleware!"); 
     } else { 
      $body = array('user_from_middleware' => $uri_array[2]); 
      $env['slim.input'] = json_encode($body); 
     } 

     $this->next->call(); 
    } 
} 

/////////////////////////////////////////////////////////////////////////////////// 

$app = new \Slim\Slim(); 

$app->view(new \JsonApiView()); 
$app->add(new \JsonApiMiddleware()); 
$app->add(new myMiddleware()); 

$app->get('/user/:id', function($id) use ($app) { 
    if ($id == 99) { 
     throw new \Exception("User $id, you are not welcome!"); 
    } else { 
     $body = json_decode($app->request->getBody()); 
     $body->msg = "User $id welcome to my API!"; 
     $app->render(200,(array) $body); 
    } 
}); 

這裏有一個請求錯過這兩個例外:

$ curl http://localhost:8082/test.php/user/1 
{"user_from_middleware":"1","msg":"User 1 welcome to my API!","error":false,"status":200} 

...這一個火災路由異常,顯示出JsonApiMiddleware工作:

$ curl http://localhost:8082/test.php/user/99 
{"msg":"ERROR: User 99, you are not welcome!","error":true,"status":500} 

...但是當這一次火災myMiddleware異常的API沒有返回值:

$ curl http://localhost:8082/test.php/user/98 
$ 

...我可以從日誌異常被拋出絕對看:

[Mon Nov 7 21:54:08 2016] PHP Fatal error: Uncaught exception 'Exception' with message 'User 98, you are not even welcome in middleware!' in /path/to/test.php:14 
Stack trace: 
#0 /path/to/vendor/slim/slim/Slim/Slim.php(1302): myMiddleware->call() 
#1 /path/to/test.php(42): Slim\Slim->run() 
#2 {main} 
    thrown in /path/to/test.php on line 14 

我錯過了什麼?再次道歉,如果這是一個乏味的問題。

回答

0

也許你應該在MyMiddlware$this->next->call()如果一個異常被拋出?..

class myMiddleware extends Middleware 
{ 
    public function call() 
    { 
     $uri_array = explode('/', $this->app->request->getResourceUri()); 
     $env = $this->app->environment; 

     if($uri_array[2] == 98) { 
      throw new \Exception("User $uri_array[2], you are not even welcome in middleware!"); 
     } else { 
      $body = array('user_from_middleware' => $uri_array[2]); 
      $env['slim.input'] = json_encode($body); 
      $this->next->call(); // call next callable only if exception was not thrown 
     } 
    } 
} 

好像你正在使用超薄v 2,但是這是我在超薄3.5做。*

+0

感謝Georgy的快速回復!我只是嘗試將next-> call()移動到else {}塊中,我擔心它仍然不像我期望的那樣行事(儘管響應稍有不同 - 我得到3個空格和一個換行符)。 – hicksde

+0

你覺得答案有幫助嗎? –

+0

(對不起,在5分鐘評論編輯超時之前有偏差)..(顯然,我希望/期待異常由'JsonApiMiddleware .__ construct()'中的'$ app-> error'代碼處理,因此導致JSON格式化狀態500返回) – hicksde

相關問題