2017-03-06 39 views
0

我有这样的代码中,匹配部分航线从JSON文件来认为:Silex的触发404错误匹配回调

$this->app->match($routePattern, function(Request $request, \Silex\Application $app) use($routeIdent, $templateConfig) { 

      //now here i need to check some conditions and, 
      //in case, trigger 404 error, but without redirect!!!! 

      if($templateConfig['test'] === true){ 
      //----> TRIGGER 404 
      } 

    }); 

有一种方法来处理呢?

回答

1

我不确定是否正确理解您的问题。你是否想在404中实现404处理?一种解决方案就是在那里回复回复。例如。如果你使用使用的Symfony \分量\ HttpFoundation \ Response对象和内置的树枝服务,您可以做到这一点像:

use Symfony\Component\HttpFoundation\Response  

$this->app->match($routePattern, function(Request $request, \Silex\Application $app) use($routeIdent, $templateConfig) { 

    if($templateConfig['test'] === true){ 
     //----> TRIGGER 404 
     return new Response($app['twig']->render('404.html.twig'), 404); 
    } 
}) 

我希望那是你所期待的。

1

您可以使用abort()

return $app->abort(404); 

返回时并不需要,因为它抛出,我只是喜欢用它作为信令,但它作为一个出口点的方式。