【问题标题】:In Symfony2, how to use Multiple Routes in Default Controller在 Symfony2 中,如何在默认控制器中使用多个路由
【发布时间】:2013-08-28 14:50:03
【问题描述】:

我一直在尝试将多个路由映射到默认控制器,但它似乎没有按预期工作。

我在控制器中使用注释:

/**
 * @Route("/", name="index_controller");
 * @Template("SeoSlinkyBundle:Default:index.html.twig");
 */
 public indexAction() {}

但我想这样做:

/**
 * @Route("/", name="index_controller");
 * @Route("/{timeoption}", name="index_controller");
 * @Template("SeoSlinkyBundle:Default:index.html.twig");
 */
 public indexAction($timeoption = "today") {
   echo $today;
   exit;
 }

这确实有效,如果我去:

http://myapp/hello

控制器回显“你好”

但如果我去

http://myapp/

控制器应该回显“今天”

但是我得到了这个错误:

无法从“/usr/share/www/myapp/app/config/routing.yml”导入资源“/usr/share/www/myapp/src/MyAppBundle/Controller/”。

这些是routing.yml的内容

my_app:
    resource: "@MyAppBundle/Controller/"
    type:     annotation
    prefix:   /
imag_ldap:
    resource: "@IMAGLdapBundle/Resources/config/routing.yml"

任何帮助将不胜感激!

【问题讨论】:

    标签: symfony


    【解决方案1】:

    我的猜测是因为您有 2 条同名路线,这是错误的

    你应该试试:

    /**
     * @Route("/", name="index_controller");
     * @Route("/{timeoption}", name="index_controller_timeoption");
     * @Template("SeoSlinkyBundle:Default:index.html.twig");
     */
     public indexAction($timeoption = "today") 
     {
         echo $today;
         exit;
     }
    

    【讨论】:

    • 正是我的建议 - 你更快 :)
    • 这很有意义!!我从来没有想过,它是如此简单,让我觉得有点愚蠢。
    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多