【问题标题】:Symfony3 - normal route is interpreted as a route with parameters/slugsSymfony3 - 普通路由被解释为带有参数/slugs的路由
【发布时间】:2016-06-01 14:00:48
【问题描述】:

我的应用中定义了以下路线:

-------------------------- ---------- -------- ------ -----------------------------------
 Name                       Method     Scheme   Host   Path
-------------------------- ---------- -------- ------ -----------------------------------
 _wdt                       ANY        ANY      ANY    /_wdt/{token}
 _profiler_home             ANY        ANY      ANY    /_profiler/
 _profiler_search           ANY        ANY      ANY    /_profiler/search
 _profiler_search_bar       ANY        ANY      ANY    /_profiler/search_bar
 _profiler_info             ANY        ANY      ANY    /_profiler/info/{about}
 _profiler_phpinfo          ANY        ANY      ANY    /_profiler/phpinfo
 _profiler_search_results   ANY        ANY      ANY    /_profiler/{token}/search/results
 _profiler                  ANY        ANY      ANY    /_profiler/{token}
 _profiler_router           ANY        ANY      ANY    /_profiler/{token}/router
 _profiler_exception        ANY        ANY      ANY    /_profiler/{token}/exception
 _profiler_exception_css    ANY        ANY      ANY    /_profiler/{token}/exception.css
 _twig_error_test           ANY        ANY      ANY    /_error/{code}.{_format}
 api_route                  ANY        ANY      ANY    /api
 sec_events_index           GET        ANY      ANY    /sec/events/
 sec_events_new             GET|POST   ANY      ANY    /sec/events/new
 sec_events_show            GET        ANY      ANY    /sec/events/{id}
 sec_events_edit            GET|POST   ANY      ANY    /sec/events/{id}/edit
 sec_guest_delete           ANY        ANY      ANY    /sec/guest/{id}
 sec_events_delete          DELETE     ANY      ANY    /sec/events/{id}
 pub_event                  ANY        ANY      ANY    /{id}/{guestid}
 home_page                  ANY        ANY      ANY    /
 about_page                 ANY        ANY      ANY    /about
 products_route             ANY        ANY      ANY    /products
 sec_guests_new             GET|POST   ANY      ANY    /sec/guests/new
 sec_guests_edit            GET|POST   ANY      ANY    /sec/guests/{id}/edit
 send_invite                ANY        ANY      ANY    /sec/invites
 admin_index                GET        ANY      ANY    /admin/users
 profile_show               ANY        ANY      ANY    /sec/profile
 admin_edit                 GET|POST   ANY      ANY    /admin/{id}/edit
 sec_delete                 DELETE     ANY      ANY    /admin/{id}
 login_route                ANY        ANY      ANY    /login
 login_check                ANY        ANY      ANY    /login_check
 pass_reset                 ANY        ANY      ANY    /reset
 pass_reset_form            ANY        ANY      ANY    /{token}
 test                       ANY        ANY      ANY    /test
 homepage                   ANY        ANY      ANY    /
 logout                     ANY        ANY      ANY    /logout
-------------------------- ---------- -------- ------ -----------------------------------

每当我通过浏览器使用/sec/profile 访问路由profile_show 时,分析器告诉我它会尝试访问路由pub_event,就好像我在浏览器中输入了/{id}/{guestid} 一样。

我做错了什么以使其选择正确的路线以及正确的控制器和方法吗?

【问题讨论】:

    标签: routing routes slug symfony


    【解决方案1】:

    您需要将您的路线pub_event 移动到routing.yml 中导入路线的末尾。

    这是正常行为,因为 /sec/profile 匹配 /{id}/{guestid}。好的做法是在 routing.yml 文件的末尾加载通用路由。

    您可以做的其他事情是在pub_event 路由中设置id 参数的要求。

    使用注释应该是这样的:

    /**
     * @Route("/{id}/{guestid}", requirements={"id" = "\d+"}, defaults={"id" = 1})
     */
    

    记住“早期的路线总是赢”。阅读 Symfony 路由一书中有关要求的更多信息:http://symfony.com/doc/current/book/routing.html#adding-requirements

    【讨论】:

    • 怎么强调都不过分。与其像这样将所有路由都放在路由文件的末尾,如果您使用注释可能无法正常工作,您应该对路由设置更严格的要求,以便某些路由可能与其他路由不匹配。我建议将您的 pub_event 路由更改为 /event/{id}/{guestid} 以获得额外的保险和 SEO 清晰度。
    • Prefixed routes 在这种情况下是个好主意:)。
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多