2016-02-19 63 views
1

我已经在我的应用程序定义了以下路线路线:Symfony3 - 正常路线被解释为与参数/蛞蝓

-------------------------- ---------- -------- ------ ----------------------------------- 
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 
-------------------------- ---------- -------- ------ ----------------------------------- 

每当我通过浏览器访问路线profile_show/sec/profile探查告诉我它试图访问路线pub_event就好像我在我的浏览器中输入了/{id}/{guestid}

有没有什么我做错了,使它拿起正确的路线和正确的控制器和方法?

回答

2

您需要在routing.yml的导入路线末尾移动路线pub_event

这是正常行为,因为/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

+3

不能强调这一点。而不是像所有这样的路线放在路由文件的末尾,如果您使用注释,这可能不起作用,您应该在您的路由上设置更严格的要求,以便某些路由可能与其他路由不匹配。我建议将'pub_event'路线更改为'/ event/{id}/{guestid}'以获得额外的保险和SEO清晰度。 –

+0

[前缀路由](http://symfony.com/doc/current/book/routing.html#prefixing-imported-routes)在这种情况下是个好主意:)。 –