【问题标题】:How to remove first URI parameter from routes using | Laravel如何使用 | 从路由中删除第一个 URI 参数拉拉维尔
【发布时间】:2019-06-15 15:16:03
【问题描述】:

我的 routes/web.php 中有以下路线。

Route::get('page/{slug}', 'PageController@index')->name('front.page.slug');
Route::get('inquiry/contact-us', 'ComplaintController@index')->name('front.complaint');
Route::get("product/{product}", 'ProductController@show')->name('front.get.product');

它的工作e.g.

当我更改路线时,它向我显示产品未在页面模型中退出的错误。

Route::get('{slug}', 'PageController@index')->name('front.page.slug');
Route::get('contact-us', 'ComplaintController@index')->name('front.complaint');
Route::get("{product}", 'ProductController@show')->name('front.get.product');

我不知道如何使用没有资源名称的 slug 开始路由,以获取 SEO 友好的 URL E.g.

【问题讨论】:

  • 由于 {slug} 和 {product} 是冲突的路线,即它们都捕获相同的 url,因此您试图实现的目标是不可能的
  • 你是对的,还有其他方法可以实现吗?
  • 你需要有一个catchall路由。在另一个问题中查看我的answer
  • @Mozammil 是正确的,或者例如,如果您的产品和 slug 可以通过正则表达式区分,您可以使用类似 {product:[0-9]+}
  • @Mozammil 这很有意义。谢谢

标签: laravel


【解决方案1】:

你可以像这个例子那样做:

Route::get('page-{slug}', 'PageController@index')->name('front.page.slug');
Route::get('contact-us', 'ComplaintController@index')->name('front.complaint');
Route::get("product-{product}", 'ProductController@show')->name('front.get.product');

您应该考虑两条路线之间的差异。

{product} 和 {slug} 是一样的

【讨论】:

    【解决方案2】:
    Route::get('{slug}', 'PageController@index')->name('front.page.slug');
    Route::get('contact-us', 'ComplaintController@index')->name('front.complaint');
    

    如果你喜欢。联系我们页面在 front.page.slug 路由中执行。

    您不能在路由文件中使用两个变量。

    像这样:

    Route::get('contact-us', 'ComplaintController@index')->name('front.complaint');
    Route::get('{slug}', 'WebController@index')->name('front.page.slug');
    

    并确定控制器中 {slug} 的类别。是页面还是产品?在同一个控制器中执行此操作。否则只有第一个 {} 在路由文件中起作用。

    【讨论】:

      【解决方案3】:

      laravel 不会将 {slug} 和 {product} 视为两条不同的路由

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-20
        • 2020-03-03
        • 2019-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        相关资源
        最近更新 更多