Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:使用前缀和登录路由_Php_Symfony - Fatal编程技术网

Php Symfony2:使用前缀和登录路由

Php Symfony2:使用前缀和登录路由,php,symfony,Php,Symfony,我正在设置允许以下路由的路由: /client_a/dashboard/ /client_b/dashboard/ 我在我的app/config/routing.yml中使用的前缀如下: AcmeMainBundle: resource: "@AcmeMainBundle/Resources/config/routing.yml" prefix: /{client} 我遇到的问题是,登录路由似乎与前缀有问题。以下是我要导入主路由文件的/Resources/config

我正在设置允许以下路由的路由:

/client_a/dashboard/
/client_b/dashboard/
我在我的
app/config/routing.yml
中使用的前缀如下:

AcmeMainBundle:
    resource: "@AcmeMainBundle/Resources/config/routing.yml"
    prefix:   /{client}  
我遇到的问题是,登录路由似乎与前缀有问题。以下是我要导入主路由文件的
/Resources/config/routing.yml
中的条目:

acme_login:
    pattern: /login/
    defaults: {_controller: AcmeMainBundle:Main:login }

acme_login_check:
    pattern: /login_check
    # defaults: This is not required since the Firewall will handle this

acme_logout:
    pattern: /logout/
    # defaults: This is not required since the Firewall will handle this
登录页面显示良好,但在用户提交登录页面后,Symfony抛出一个错误,指出:

Unable to find the controller for path "/client_a/login_check". Maybe you forgot to add the matching route in your routing configuration?
在我看来,Symfony2在内部安全路由和在routing.yml中使用前缀方面遇到了困难

有什么办法克服这个问题吗

注意:解决此问题的一种方法是更改my routing.yml文件中的所有路由以包含{client}参数。唯一的问题是,这是一个非常广泛的应用程序,有大量的路线。除了登录过程中的安全处理之外,使用前缀也能带来奇迹

谢谢


JB

我认为您可能需要在这些路由上使用
默认值定义控制器:{u controller:AcmeMainBundle:Main:login}
。在默认情况下,它不需要它们,因为防火墙会在路由之前捕获它们。但是在这种情况下,它可能需要它来匹配前缀。

经过相当多的研究和尝试不同的方面后,在路由中使用动态前缀似乎没有很好的解决方案

问题似乎是安全包在路由发生之前执行其代码,因此前缀中的路由属性在security.yml中不可用,而是静态路径。这本身就造成了问题,因为Symfony随后抱怨缺少属性等


因此,答案是,结合安全实现,上述方法是不可能的,至少在我发现的情况下是不可能的。

您需要的可能解决方案是:

文件:src/Acme/CoreBundle/Twig/PathExtension.php

<?php

namespace Acme\CoreBundle\Twig\Extension;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;

class PathExtension extends \Twig_Extension
{

    private $request;
    private $router;

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

    public function onKernelRequest(GetResponseEvent $event) {
        if ($event->getRequestType() === HttpKernel::MASTER_REQUEST) {
            $this->request = $event->getRequest();
        }
    }

    public function getFunctions()
    {
        return array(
            'path_route' => new \Twig_Function_Method($this, 'getPath')
        );
    }

    public function getPath($name, $parameters = array())
    {
        $parameters = array_merge($parameters, [
            'client' => $this->request->get('client'),
        ]);

        return $this->router->generate($name, $parameters, false);
    }

    public function getName()
    {
        return 'twig_path_route_extension';
    }

}
设置路由选项:

文件:app/config/routing.yml

_acme_client:
    resource: "@AcmeCoreBundle/Resources/config/routing.yml"
    prefix:   /{client}/
然后您可以在模板和routing.yml中使用它,例如:

_acme_client_dashboard:
    path:     /dashboard
    defaults: { _controller: AcmeCoreBundle:System:dashboard }
然后,在模板中,您可以使用:

<a href="{{ path_route('_acme_client_dashboard') }}">
  Link to Dashboard.
</a>

参考资料:


是的,考虑覆盖控制器中的默认安全层处理。不过还有很多东西需要挖掘,我希望能够依靠Symfony自己的内置功能。如果我为
login\u check
指定默认控制器(请注意,登录操作似乎工作正常),我将开始收到一系列其他错误,因为缺少内容。
<a href="{{ path_route('_acme_client_dashboard') }}">
  Link to Dashboard.
</a>