2016-11-07 67 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

相关问题