2017-04-06 45 views
1

在/src/AppBundle/Controller/CustomExceptionController.php我:訪問redirectToRoute在CustomExceptionController

namespace AppBundle\Controller; 

use Symfony\Component\Debug\Exception\FlattenException; 
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 

class CustomExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController 
{ 

    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) 
    { 
     return $this->redirectToRoute('custom_error'); //not working 
    } 
} 

這不工作,因爲\的Symfony \包\ TwigBundle \控制器\ ExceptionController不擴展類控制器。那麼如何在這個類中使用$ this-> redirectToRoute?

回答

3

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); 
} 
0

使用UrlGeneratorInterface像

getContainer()->get('router')->generate('custom_error', ['key' => 'some val'], 0); 

getContainer()簡單的方法 - 是自己本功能,請參閱手冊If you need to generate a URL from a service, type-hint the UrlGeneratorInterface service:

// src/Service/SomeService.php 

use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 

class SomeService 
{ 
    private $router; 

    public function __construct(UrlGeneratorInterface $router) 
    { 
     $this->router = $router; 
    } 

    public function someMethod() 
    { 
     $url = $this->router->generate('custom_error', [ 'key' => 'some value' ]); 

    } 
}