【问题标题】:Symfony2: How to get the route prefixSymfony2:如何获取路由前缀
【发布时间】:2015-09-22 05:52:06
【问题描述】:

我想知道的是,我有一个管理面板,我可以使用“admin”前缀访问它,但只有先进入配置页面才能访问它。

为此,我创建了如下监听器事件:

<?php

namespace Config\ConfigBundle\Listener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class ConfigListener {

public function __construct(ContainerInterface $container){

    $this->router = $container->get('router');
    $this->em = $container->get('doctrine')->getEntityManager();;

}

public function onKernelRequest(GetResponseEvent $event)
{
    $route = $event->getRequest()->attributes->get('_route');
    if ( $route == 'admin') {

        $config = $this->em->getRepository('ConfigBundle:Config')->findConfig();
        if($config == null){
            $event->setResponse(new RedirectResponse($this->router->generate('adminConfig')));
        }   
    } 
}

}

这段代码工作正常,但是它只获取名为“admin”的路由,我想要检查该路由的前缀是否等于“admin”以重定向到配置页面。

我错过了一些东西,我不知道如何解决这个问题......

【问题讨论】:

    标签: php symfony router


    【解决方案1】:

    您可以使用以下 sn-p 检查路由是否包含 'admin' 前缀:

    if (0 == strpos($route, 'admin')) {
        // perform redirect
    }
    

    【讨论】:

    • 谢谢,我添加了 ==> && $route != 'adminConfig' 以避免循环,但是现在,我知道资源作为 css 和东西没有加载......我得到了302 错误...
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多