Reputation: 18850
I am new to Silex. I am trying to pass Request $request
to $app->error(...){...}
. Normally it would look like so:
$app->error(function(\Exception $e, $code) use ($app) { ...
I want to use Request within the error controller. The code below will generate en error. Any idea how to snick the Request $request
object into this controller ? so I will have access to 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();
Upvotes: 3
Views: 730
Reputation: 18850
$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();
Now I can redirect users when 404 based on the area they are in - to different predefined paths.
Upvotes: 4
Reputation: 36
$this->error(
function (\Exception $e, Request $request, $code) {
//yours code here
}
);
Upvotes: 0