路由設(shè)置相關(guān)問題
洪老您好,我今天調(diào)試了一天路由的配置,有幾個(gè)問題還是沒有想明白,請洪老解惑。上代碼:
第一個(gè)問題:
#app/config/routing.yml中路由配置 custom: ????resource:?@CustomSiteBundle/Controller/ ????type:?????annotation
#src/Custom/SiteBundle/Controller/DefaultController.php namespace?Custom\SiteBundle\Controller; use?Symfony\Bundle\FrameworkBundle\Controller\Controller; //?these?import?the?"@Route"?and?"@Template"?annotations use?Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use?Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; class?DefaultController?extends?Controller { ????/** ?????*?@Route("/",?name="_demo") ?????*?@Template() ?????*/ ????public?function?indexAction() ????{ ????????return?array(); ????} }
此時(shí)可以正常訪問該路由:http://localhost/app.php,但是我新建一個(gè)Controller:
#src/Custom/SiteBundle/Controller/CommunalController.php namespace?Site\HomeBundle\Controller; use?Symfony\Bundle\FrameworkBundle\Controller\Controller; //?these?import?the?"@Route"?and?"@Template"?annotations use?Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use?Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; /** ?*?@Route("/communal") ?*/ class?CommunalController?extends?Controller { ????/** ?????*?@Route("/navgate") ?????*?@Template() ?????*/ ???? ????public?function?naygateAction() ????{ ????????return?array(); ????} ???? ????/** ?????*?@Route("/sidebar") ?????*?@Template() ?????*/ ????public?function?sideBarAction() ????{ ????????return?array(); ????} ???? ????/** ?????*?@Route("/footer") ?????*?@Template() ?????*/ ????public?function?footerAction() ????{ ????????return?array(); ????} }
此時(shí)訪問/communal下的任何一個(gè)方法都報(bào)404,http://localhost/app_dev.php/communal
查看官方文檔有:
Including External Routing Resources
All routes are loaded via a single configuration file - usually app/config/routing.yml (see Creating Routes above). However, if you use routing annotations, you'll need to point the router to the controllers with the annotations. This can be done by "importing" directories into the routing configuration:
但不知我這錯(cuò)在那塊?
還有app/config/routing.yml中custom指的是什么?和@Route("/", name="_demo")中的name是什么關(guān)系?
第二個(gè)問題:
#修改app/config/routing.yml custom: ????resource:?"@CustomSiteBundle/Resources/config/routing.yml"
#CustomSiteBundle/Resources/config/routing.yml custom: ????resource:?@CustomSiteBundle/Controller/ ????type:?????annotation
修改后訪問http://localhost/app_dev.php,404
這塊又是為什么,我只是換了位置?
請洪老答疑,我覺得路由這塊洪老一筆帶過,尤其是app/config/routing.yml與其他bundle的routing.yml之間關(guān)系講的很簡單,難道也是伏筆?
2015-01-27
1.其實(shí)你不用試來試去,每個(gè)action最終的訪問路徑可以通過命令行工具查看,app/console router:debug
2.我不建議把yml里的key寫成一樣的,也就是說你第一個(gè)custom和第二個(gè)custom最好換成兩個(gè)不重復(fù)的名字。判斷routing是否設(shè)置成功,配置是否載入成功等等,最好的辦法還是通過上面一條中說到的router:debug去分析,如果這個(gè)命令中出現(xiàn)了Path為/的路由,那么你的http://localhost/app_dev.php肯定是可以打開的,如果沒有出現(xiàn),則必定是404。
2015-01-27
補(bǔ)充一點(diǎn),在routing.yml中定義路由和在annotation中定義路由是二選其一的兩種定義路由的方式,所以雖然Route里可以定義name,但實(shí)際操作上并不會(huì)沖突。
2015-01-27
custom就是指這個(gè)路由的名字,將來在模板中或者在controller中如果要生成指向某一個(gè)路由的鏈接,你就會(huì)用到這個(gè)名字。