Error handling Silex-如何将请求$Request传递给$app->;错误(函数(\Exception$e,$code)使用($app){
我是Silex的新手。我正在尝试将Error handling Silex-如何将请求$Request传递给$app->;错误(函数(\Exception$e,$code)使用($app){,error-handling,request,silex,Error Handling,Request,Silex,我是Silex的新手。我正在尝试将Request$Request传递到$app->error(…){…}。通常情况如下: $app->error(function(\Exception $e, $code) use ($app) { ... 我想在错误控制器中使用Request。下面的代码将生成en error。知道如何将Request$Request对象剪切到此控制器中吗?这样我就可以访问Request->getPathInfo() //... $app->error(func
Request$Request
传递到$app->error(…){…}
。通常情况如下:
$app->error(function(\Exception $e, $code) use ($app) { ...
我想在错误控制器中使用Request。下面的代码将生成en error。知道如何将Request$Request
对象剪切到此控制器中吗?这样我就可以访问Request->getPathInfo()
//...
$app->error(function(\Exception $e, $code, Request $request) use ($app) {
if (404 === $code) {
$path = $request->getPathInfo();
$path = explode('/',$path);
if($path[1] == 'php'){
return $app->redirect($app['url_generator']->generate('php'));
}
if($path[1] == 'css'){
return $app->redirect($app['url_generator']->generate('css'));
}
//...
return $app->redirect($app['url_generator']->generate('home'));
}
// Do something else (handle error 500 etc.)
});
// RUN
$app->run();
$path=$app['request']->getPathInfo();
$app->error(function(\Exception $e, $code) use ($app) {
if (404 === $code) {
$path = $app['request']->getPathInfo();
$path = explode('/',$path);
echo $path[1];
if($path[1] == 'php'){
return $app->redirect($app['url_generator']->generate('php'));
}
if($path[1] == 'css'){
return $app->redirect($app['url_generator']->generate('css'));
}
//...
return $app->redirect($app['url_generator']->generate('home'));
}
// Do something else (handle error 500 etc.)
});
// RUN
$app->run();
现在我可以在404时根据用户所在的区域将用户重定向到不同的预定义路径。试试下面的代码
Request::createFromGlobals()
虽然此代码片段可能会解决此问题,但它不会解释为什么或如何回答此问题。请注意,这确实有助于提高您文章的质量。请记住,您将在将来为读者回答此问题,而这些人可能不知道您提出代码建议的原因。标记者/审阅者:
$this->error(
function (\Exception $e, Request $request, $code) {
//yours code here
}
);