我无法弄清楚,为什么我使用Laravel route()函数生成的链接看起来不像我想要的。
这是我的Routes.php的行代码
Route::get('/market/announcements/show/{period}/{date}', array('as' => 'market.announcements.index', 'uses' => 'AnnouncementsController@index'));
Route::get('/market/announcements/history', array('as' => 'market.announcements.history', 'uses' => 'AnnouncementsController@history'));
Route::resource('market/announcements', 'AnnouncementsController');
以下是我如何生成链接:
route('market.announcements.index', ['period' => $key, 'date' => $link])
不幸的是,生成的链接是: ?市场/通告期间=年&安培;日期= 2016
而不是: 市场/公告/显示/年/ 2016
答案 0 :(得分:0)
这是因为
Route::resource('market/announcements', 'AnnouncementsController');
覆盖路线
Route::get('/market/announcements/show/{period}/{date}', array('as' => 'market.announcements.index', 'uses' => 'AnnouncementsController@index'));
如果您运行php artisan route:list
,则会看到
+-----------+-------------------------------------------+------------------------------+ | Method | URI | Name | +-----------+-------------------------------------------+------------------------------+ | POST | market/announcements | market.announcements.store | | GET|HEAD | market/announcements | market.announcements.index | | GET|HEAD | market/announcements/create | market.announcements.create | | GET|HEAD | market/announcements/history | market.announcements.history | | GET|HEAD | market/announcements/show/{period}/{date} | market.announcements.index | | GET|HEAD | market/announcements/{announcements} | market.announcements.show | | PUT|PATCH | market/announcements/{announcements} | market.announcements.update | | DELETE | market/announcements/{announcements} | market.announcements.destroy | | GET|HEAD | market/announcements/{announcements}/edit | market.announcements.edit |
请参阅列出的market.announcements.index
两次? Router
取最后一个。
现在,如果您暂时将路径文件中的资源注释掉
// Route::resource('market/announcements', 'AnnouncementsController');
并会在tinker
中尝试,您会看到您的路线现在按预期工作
$ php artisan tinker Psy Shell v0.7.2 (PHP 5.6.19 — cli) by Justin Hileman >>> route('market.announcements.index', ['period' => 'year', 'date' => 2016]); => "http://localhost/market/announcements/show/year/2016"