redirectToRoute
是你提到的Controller
類的一部分。 您所需要做的就是自己創建方法。
首先,您需要在路由器注入到你的CustomExceptionController
(因此你需要定義您的自定義控制器作爲DI服務)
services:
my.custom.exception_controller:
class: CustomExceptionController
arguments: [ "@twig", "%kernel.debug%", "@router" ]
twig:
exception_controller: my.custom.exception_controller:showAction
自定義類應該是這樣的:
class CustomExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController
{
protected $router;
public function __construct(\Twig_Environment $twig, $debug, Router $router)
{
parent::__construct($twig, $debug);
$this->router = $router;
}
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
}
}
之後,您可以像在控制器中那樣在CustomExceptionController中實現redirectToRoute(或者直接創建RedirectResponse而不直接使用幫助器方法)
/**
* Returns a RedirectResponse to the given URL.
*
* @param string $url The URL to redirect to
* @param int $status The status code to use for the Response
*
* @return RedirectResponse
*/
public function redirect($url, $status = 302)
{
return new RedirectResponse($url, $status);
}
/**
* Returns a RedirectResponse to the given route with the given parameters.
*
* @param string $route The name of the route
* @param array $parameters An array of parameters
* @param int $status The status code to use for the Response
*
* @return RedirectResponse
*/
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
{
return $this->redirect($this->router->generateUrl($route, $parameters), $status);
}
來源
2017-04-06 09:00:14
Joe