【问题标题】:Laravel - Routes not working (Not found)Laravel - 路线不起作用(未找到)
【发布时间】:2018-09-12 11:27:18
【问题描述】:

我有一条工作路线

Route::get('/{url}', 'Controller@view)->name('view')->where('url', '[\w\d\-]+(.*)');

这完美地工作并显示如下页面(只是演示页面):

我在这个页面上有一个链接可以下订单,我想创建一个路由

Route::get('/{url}/order', 'Controller@order)->name('order')->where('url', '[\w\d\-]+(.*)');

查看刀片

<a href="{{ route('order', $product['url']) }}">Order Now</a>

php artisan route:list

|    | GET|HEAD | {slug}        | view   | App\Http\Controllers\Controller@view    | web   |
|    | GET|HEAD | {slug}/order  | order  | App\Http\Controllers\Controller@order   | web   |

每当我点击这个链接时,Laravel 都会返回 Not Found Error。什么是问题?我重新启动服务器并检查了 Mod_rewrite。一切都很好。

【问题讨论】:

  • 切换 2 条路线的位置。{slug}/order 转到 {{slug}},因为它符合要求。如果您将订单移到常规 slug 之上,它应该可以工作
  • @universal : 请显示您对 $product['url'] 的价值
  • @Taacoo 解决方案有效!
  • 我会做一个答案,如果你想要@universal,你可以接受

标签: php laravel laravel-5 routes


【解决方案1】:

路线从上到下工作。 Laravel 搜索任何符合给定 URL 的内容。

当使用{{slugs}} 或任何其他参数时,关键是将范围最广的键放在路由的底部。

例子:

Route::get('/{url}', 'Controller@index);
Route::get('/{url}/order', 'Controller@order);

有了这个设置。 ALL 路由将转到您的 Controller@index 方法。由于您的 {{url}} 捕获了所有内容

通过切换 2,您的更严格的路线首先会在具有非常广泛要求的 url 之前得到满足。

更多信息请查看laravel docs

【讨论】:

    【解决方案2】:

    我认为您在代码中忘记了 ' 更改您的代码

    Route::get('/{url}/order', 'Controller@order)->name('order')->where('url', '[\w\d\-]+(.*)');
    

    Route::get('/{url}/order', 'Controller@order')->name('order')->where('url', '[\w\d\-]+(.*)');
    

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2013-05-03
      • 2014-09-08
      • 2015-03-18
      • 2017-12-13
      相关资源
      最近更新 更多