我是Silex的新手。我正在尝试将Request $request
传递给$app->error(...){...}
。通常它看起来像这样:
$app->error(function(\Exception $e, $code) use ($app) { ...
我想在错误控制器中使用Request。下面的代码将生成错误。知道如何将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();
答案 0 :(得分:3)
$ 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();
现在,我可以根据用户所在的区域将用户重定向到不同的预定义路径。
答案 1 :(得分:0)
$this->error(
function (\Exception $e, Request $request, $code) {
//yours code here
}
);
答案 2 :(得分:-1)
试试这段代码,
Request::createFromGlobals()