2016-02-19 61 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)在這種情況下是個好主意:)。 –