我希望这只是我在文档中忽略的一种简单情况。我正在重构我们的Web应用程序以利用URL中的段。我们公司允许许多组织进行注册,每个组织都有自己的页面和子页面。我正在尝试完成以下操作:
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/', 'IndexController@index');
Route::get('/dashboard', 'DashboardController@index');
但是,如何在不与其他路线冲突的情况下做到这一点?例如,如果我有
'/{organization-slug}'
,那么它也适用于任何根级路由。因此,如果用户转到/dashboard
,他们将被路由到OrganizationController@index
而不是DashboardController@index
laravel是否具有内置功能来处理这种情况?
编辑
作为对某些回答的回应,指出路由文件的顺序需要修改。我创建了一个新的laravel项目进行测试,并将以下路由添加到
/routes/web.php
Route::get('/{some_id}', function($some_id){
echo $some_id;
});
Route::get('/{some_id}/{another_id}', function($some_id, $another_id){
echo $some_id . ' - ' . $another_id;
});
Route::get('/hardcoded/subhard', function(){
echo 'This is the value returned from hardcoded url with sub directory';
});
Route::get('/hardcoded', function(){
echo 'This is the value returned from hardcoded url';
});
永远不会到达路由
/hardcoded/subhard
和/hardcoded
。使用此命令时。但是,如果将静态路线移动到动态上方,如下所示:Route::get('/hardcoded/subhard', function(){
echo 'This is the value returned from hardcoded url with sub directory';
});
Route::get('/hardcoded', function(){
echo 'This is the value returned from hardcoded url';
});
Route::get('/{some_id}', function($some_id){
echo $some_id;
});
Route::get('/{some_id}/{another_id}', function($some_id, $another_id){
echo $some_id . ' - ' . $another_id;
});
然后,适当的路由似乎按预期方式工作。这样对吗?
最佳答案
顺序在路径文件中很重要。
最通用的放在最后。
编辑:
关于php - Laravel在 route ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46281063/