我希望事实证明这是一个我在文档中忽略的简单情况。我正在重构我们的Web应用程序以利用网址中的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;
});
然后适当的路线似乎按预期工作。这是对的吗?
答案 0 :(得分:2)
Laravel认为与有效路由相同的资源的最后一个路由定义。所以只需将Route::get('/dashboard', 'DashboardController@index');
放在slug路径的定义之后:
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');
答案 1 :(得分:1)
您可以使用Regular Expression Constraints作为路线。
在参数化路线的末尾添加->where('organization-slug', '^(?!.*dashboard).*$')
,它们将适用于除“信息中心”之外的任何slu g,并且信息中心路线也可以安全地用于http://yourdomain.com/dashboard
。
Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard).*$');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage')->where('organization-slug', '^(?!.*dashboard).*$');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');
如果您有其他路线,例如仪表板,您也可以添加它们。
Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard|.*dashboard1|.*dashboard2|.*dashboard3).*$');
答案 2 :(得分:1)
订单在路线文件中很重要。 把最通用的放在最后。
编辑:
Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');
答案 3 :(得分:0)
将
ERROR: no such target '//:bar-src.jar': target 'bar-src.jar' not declared in package '' (did you mean 'libbar-src.jar'?) defined by /Users/.../java_project/BUILD.
放在路径文件的顶部:
java_library