【问题标题】:laravel 5 slug will not return pageslaravel 5 slug 不会返回页面
【发布时间】:2018-08-23 17:44:19
【问题描述】:

我尝试在 url 中没有任何附加参数的情况下获取我的路线,但我收到 Sorry, the page you are looking for could not be found. 错误。

如果我使用如下网址:domain/category/lenovo,其中 lenovo 是我的 slug,它正在工作。但是,如果我尝试获取我的网址,例如:domain/lenovo,我会在上面遇到错误。

这是我的代码:

route

Route::get('/{categoryslug}', 'frontend\FrontendController@totalcategoriessubs')->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');

function

public function totalcategoriessubs($categoryslug) {
    $categories = Category::where('slug','=',$categoryslug)->with('subcategories')->paginate(12);
    return view('front.categoriessubs', compact('categories'));
  }

它还会阻止我的其他网址,例如 domain/admin/dashboard 等。

有什么想法吗?

【问题讨论】:

  • 您的路线是否有任何前缀分配给它?
  • @linktoahref 管理部分是的,但其余的如domain/category/lenovo
  • 请添加您的完整路线文件
  • 也许这可以帮助你:stackoverflow.com/questions/48124579/…
  • @GautamPatadiya 的目的是删除那个额外的部分而不是把它移动到其他地方,你见过wordpressjoomlamagento等的网址吗?

标签: php laravel slug


【解决方案1】:

这似乎是由您的路线顺序引起的。

您的实际问题是,当您到达固定端点时,例如/dashboard,这将由Route::get('/{categoryslug}', ...) 在正确的端点(Route::get('dashboard', ...))之前处理。因此,当您的系统注意到该 slug (dashboard) 没有类别时,它会引发错误。

尝试更改路线的顺序,如下所示:

Route::get('/category/{categorySlug}', 'CategoryController@index');
Route::get('/dashboard', 'DashboardController@home');
// and so on..

Route::get('/{categoryslug}', 'frontend\FrontendController@totalcategoriessubs')
        ->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');

始终将具有固定部分的路由 (.../category/...) 放在具有动态元素的路由 (.../{categoryslug}/) 之前。

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2015-07-16
    • 2017-04-29
    • 2018-12-03
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多