【发布时间】:2018-02-27 02:17:09
【问题描述】:
我希望这是一个我刚刚在文档中忽略的简单情况。我正在重构我们的 Web 应用程序以在 url 中使用 slugs。我们公司允许许多组织注册,每个组织都有自己的页面和子页面。我正在尝试完成以下操作:
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;
});
然后,适当的路线似乎按预期工作。这是正确的吗?
【问题讨论】:
-
您可以将您的仪表板网址更改为
/user/dashboard/吗? -
我主要以该路由为例,因为在根级别还有更多类似的路由
-
您是否尝试过在组织路由之前添加“orgs”(在一个组中)之类的东西(在一个组中),然后是其他路由。我觉得这可能更稳定且不易出错。如果这不是一个选项,您可以忽略此评论