whi*_*hoa 5 php laravel laravel-5
我希望这是一个简单的情况,我在文档中忽略了.我正在重构我们的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');
Run Code Online (Sandbox Code Playgroud)
但是,如何在不与其他路线冲突的情况下执行此操作?例如,如果我有'/{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';
});
Run Code Online (Sandbox Code Playgroud)
该路线/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;
});
Run Code Online (Sandbox Code Playgroud)
然后适当的路线似乎按预期工作.它是否正确?
路径文件中的顺序很重要。把最通用的放在最后。
编辑:
Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');
归档时间: |
|
查看次数: |
4902 次 |
最近记录: |