0
我构建了kernel listener some time ago,将用户重定向到某种语言。内核监听器中的访问路由选项
有几个页面,没有翻译,并且用户不应该被重定向。当我使用JMSI18nRoutingBundle时,我想,这将是使用包中的'options:{i18n:false}'设置的最佳方式。
我需要能够读取内核侦听器中当前路由的选项。这可能吗?
我构建了kernel listener some time ago,将用户重定向到某种语言。内核监听器中的访问路由选项
有几个页面,没有翻译,并且用户不应该被重定向。当我使用JMSI18nRoutingBundle时,我想,这将是使用包中的'options:{i18n:false}'设置的最佳方式。
我需要能够读取内核侦听器中当前路由的选项。这可能吗?
对于首先获取路线选项,您必须先获取路线集合,然后从路线集合中获取路线对象,具体取决于路线名称。
因此,您的侦听器将对路由器有依赖性。你的构造函数会看起来像这样。
/**
* @var $routeCollection \Symfony\Component\Routing\RouteCollection
*/
private $_routeCollection;
function __construct(\Symfony\Bundle\FrameworkBundle\Routing\Router $router)
{
$this->_routeCollection = $router->getRouteCollection();
}
现在在您的侦听器方法中,您将需要请求对象来获取当前路由名称。例如,如果您的侦听器方法是onKernelController()
function onKernelController(FilterControllerEvent $event)
{
/**
* @var $route \Symfony\Component\Routing\Route
*/
$route = $this->_routeCollection->get($event->getRequest()->get('_route'));
// @var $allOptions will have all the options for current route.
$allOptions = $route->getOptions();
// To get specific option you can use getOption()
$someSpecificOption = $route->getOption('<key>');
}