【问题标题】:Laravel slugs in routes路线中的 Laravel 蛞蝓
【发布时间】: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”(在一个组中)之类的东西(在一个组中),然后是其他路由。我觉得这可能更稳定且不易出错。如果这不是一个选项,您可以忽略此评论

标签: php laravel laravel-5


【解决方案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');

【讨论】:

  • 这似乎没有什么不同。我已经用我在干净的 laravel 安装中所做的测试更新了我的问题。请查看详细信息。
【解决方案2】:

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');

【讨论】:

  • 这真的有用吗,因为我试过了,但没用
  • @AjayDeepakKumar。回答不被接受,因为你是正确的。这是行不通的。无论顺序如何,似乎总是使用可变路由。
  • @JeanMarcos 我已经修改了我的问题,以包括测试这个的简单测试路线。请查看详细信息。
  • 我可以看到@ThomasRift 的答案正如你所寻找的那样完美,但如果一个 slug 是 dashboard 那么它将无法工作
【解决方案3】:

您可以将Regular Expression Constraints 用于您的路线。

在参数化路由的末尾添加->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');

如果您有其他路线,例如仪表板,您也可以添加它们。

Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard|.*dashboard1|.*dashboard2|.*dashboard3).*$');

【讨论】:

  • 感谢您提供此解决方案。由于我们有许多路线,并且必须为其中很大一部分(包括所有其他路线)编写这样的规则,因此我们试图远离这种情况。但是,如果没有其他解决方案出现……这可能是我们必须采取的“路线”;)
【解决方案4】:

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');

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2015-08-27
    • 2013-02-17
    • 2015-10-23
    • 2019-08-15
    相关资源
    最近更新 更多