Laravel 在路由中插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46281063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel slugs in routes
提问by whitwhoa
I'm hoping this turns out to be a simple situation that I have just overlooked in the documentation. I am refactoring our web application to utilize slugs in urls. Our company allows many organizations to register, each having their own page and sub pages. I am trying to accomplish something like the below:
我希望这只是我刚刚在文档中忽略的一个简单情况。我正在重构我们的 Web 应用程序以在 url 中使用 slug。我们公司允许许多组织注册,每个组织都有自己的页面和子页面。我正在尝试完成以下操作:
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/', 'IndexController@index');
Route::get('/dashboard', 'DashboardController@index');
However, how can I do this without conflicting with other routes? For example if I have '/{organization-slug}'
this would also match for any root level route. So if a user goes to /dashboard
, they would be routed to OrganizationController@index
instead of DashboardController@index
但是,如何在不与其他路线冲突的情况下做到这一点?例如,如果我有'/{organization-slug}'
这也将匹配任何根级路由。因此,如果用户转到/dashboard
,他们将被路由到OrganizationController@index
而不是DashboardController@index
Does laravel have built in functionality to handle this situation?
Laravel 是否有内置功能来处理这种情况?
EDIT
编辑
In response to some of the answers stating that the order of the routes file is what needs to be revised. I have created a new laravel project to test this, and added the following routes to /routes/web.php
回应一些回答说路由文件的顺序是需要修改的。我创建了一个新的 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';
});
The routes /hardcoded/subhard
and /hardcoded
are never reached. When this order is used. However, if we move the static routes above the dynamic like below:
路线/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;
});
Then the appropriate routes appear to be working as expected. Is this correct?
然后适当的路线似乎按预期工作。这样对吗?
采纳答案by Mohammad Hassany
Order is important in route file. Put the most generic in last.
顺序在路由文件中很重要。把最通用的放在最后。
Edited:
编辑:
Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');
回答by Jean Marcos
Laravel considers the last route definition for the same resource as the valid route. So just put the Route::get('/dashboard', 'DashboardController@index');
after the definition of the route of slugs:
Laravel 将相同资源的最后一个路由定义视为有效路由。所以只需将Route::get('/dashboard', 'DashboardController@index');
slugs 路径的定义放在后面:
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');
回答by Morteza Rajabi
You can use Regular Expression Constraintsfor your routes.
您可以为您的路线使用正则表达式约束。
Add ->where('organization-slug', '^(?!.*dashboard).*$')
at the end of your parameterized routes, and they will work for any slugs except 'dashboard', and dashboard route also will work safely for http://yourdomain.com/dashboard
.
添加->where('organization-slug', '^(?!.*dashboard).*$')
在参数化路由的末尾,它们将适用于除“仪表板”之外的任何 slug,并且仪表板路由也可以安全地用于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');
And if you have other routes like dashboard you can add them as well.
如果您有其他路线,例如仪表板,您也可以添加它们。
Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard|.*dashboard1|.*dashboard2|.*dashboard3).*$');
回答by ThomasRift
Put Route::get('/dashboard', 'DashboardController@index');
to the top in the route file :
放在Route::get('/dashboard', 'DashboardController@index');
路由文件的顶部:
Route::get('/dashboard', 'DashboardController@index');
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/', 'IndexController@index');