简体   繁体   English

使用Symfony2中的动态前缀自定义根路径

[英]Customize Root Path with a dynamic Prefix in Symfony2

I have an application already done with Symfony2 framework. 我已经使用Symfony2框架完成了一个应用程序。

The current rooting in the app lok like that : 当前在应用程序中扎根的样子是这样的:

http://testserver.loca.com/app_dev.php/Dashboard/

I would like to Add a dynamic prefix like that : 我想添加这样的动态前缀:

http://testserver.loca.com/app_dev.php/Prefix/Dashboard/

the dynamic Prefix will be chosen by the user when he click on a link that will send him directly to the Specific Page, the list will look like: 用户单击链接将其直接发送到“特定页面”时,将选择动态前缀,列表如下所示:

http://testserver.loca.com/app_dev.php/Prefix1/Dashboard/
http://testserver.loca.com/app_dev.php/Prefix2/Dashboard/
http://testserver.loca.com/app_dev.php/Prefix3/Dashboard/

I found around that there are a possibility to do it with a Service and an ExtraLoader Class, but i realdon't know how to get this done. 我发现周围有可能通过Service和ExtraLoader类来实现,但是我实在不知道该如何完成。

You could handle the prefix in the routing: 您可以在路由中处理前缀:

/**
 * @Route("{prefix}/Dashboard", name="dynamic_dashboard")
 * @Template()
 */
public function dynamicDashboardAction($prefix)
{
    // Some way of deciding what to do based on prefix
    switch($prefix){
        case 'Prefix1':
            // Do stuff for prefix1 etc
        break;
        // etc
    }
}

In the above, the route has a place holder for prefix which is passed to the dynamicDashboardAction($prefix) method. 在上面的代码中,路由具有一个前缀占位符,该前缀被传递给dynamicDashboardAction($prefix)方法。 In the view, you can use twig to pass the correct prefix from each link: 在视图中,您可以使用twig从每个链接传递正确的前缀:

<a href={{ path("dynamic_dashboard", {"prefix": "Prefix1"}) }}">Dashboard 1</a>
<a href={{ path("dynamic_dashboard", {"prefix": "Prefix2"}) }}">Dashboard 2</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM