Symfony,将所有请求重定向到侦听器内的特定路由
Symfony, redirect all request to spesific route inside listener
我使用 symfony 2.8,我想将所有请求重定向到一条名为 'coming-soon' 的路由,但它对我不起作用。还有其他更好的解决方案吗?
浏览器错误
ERR_TOO_MANY_REDIRECTS
这是监听器:
kernel.listener.redirect_listener:
class: BaseBundle\Listener\RedirectListener
arguments: [ '@router' ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
这是监听器:
namespace BaseBundle\Listener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class RedirectListener
{
public function __construct(Router $router)
{
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = new Request();
$routeName = $request->get('_route');
if (!$event->isMasterRequest() and $routeName == 'coming-soon') {
return;
}
$response = new RedirectResponse($this->router->generate('coming-soon'));
$event->setResponse($response);
}
}
通过使用 $event->getRequest()->get('_route'); 而不是 $request[=15 获取路由名称来解决=]
public function onKernelRequest(GetResponseEvent $event)
{
$routeName = $event->getRequest()->get('_route');
if ($event->isMasterRequest() and $routeName == 'coming-soon') {
return;
}
$response = new RedirectResponse($this->router->generate('coming-soon'));
$event->setResponse($response);
}
我使用 symfony 2.8,我想将所有请求重定向到一条名为 'coming-soon' 的路由,但它对我不起作用。还有其他更好的解决方案吗?
浏览器错误
ERR_TOO_MANY_REDIRECTS
这是监听器:
kernel.listener.redirect_listener:
class: BaseBundle\Listener\RedirectListener
arguments: [ '@router' ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
这是监听器:
namespace BaseBundle\Listener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class RedirectListener
{
public function __construct(Router $router)
{
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = new Request();
$routeName = $request->get('_route');
if (!$event->isMasterRequest() and $routeName == 'coming-soon') {
return;
}
$response = new RedirectResponse($this->router->generate('coming-soon'));
$event->setResponse($response);
}
}
通过使用 $event->getRequest()->get('_route'); 而不是 $request[=15 获取路由名称来解决=]
public function onKernelRequest(GetResponseEvent $event)
{
$routeName = $event->getRequest()->get('_route');
if ($event->isMasterRequest() and $routeName == 'coming-soon') {
return;
}
$response = new RedirectResponse($this->router->generate('coming-soon'));
$event->setResponse($response);
}