You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel路由无法正常工作(404未找到)问题咨询

Fixing Laravel Route 404 for /url/order Route

Let me break down why you're hitting that 404 and how to fix it quickly:

The Root Cause

Your first route Route::get('/{url}', ...) has a regex constraint [\w\d\-]+(.*) that's way too broad. That pattern matches any string starting with letters/numbers/dashes, followed by literally anything (including slashes). So when you visit /your-product/order, Laravel is treating the entire your-product/order as the {url} parameter and routing it to Controller@view instead of hitting your order route. Since your view method probably doesn't handle that parameter value, you get a 404.

Solution 1: Tighten Constraints + Reorder Routes

This is the cleanest fix for most cases:

  1. Register the more specific route first: Laravel matches routes in the order they're defined, so put your order route above the view route.
  2. Restrict the {url} parameter to single segments: Update the regex to exclude slashes, so it only matches the product slug (not the full path with /order).

Update your routes like this:

// More specific order route comes FIRST
Route::get('/{url}/order', 'Controller@order')->name('order')->where('url', '[\w\d\-]+');

// General view route with tighter constraint
Route::get('/{url}', 'Controller@view')->name('view')->where('url', '[\w\d\-]+');

Solution 2: For Multi-Level {url} Paths

If you need the {url} parameter to support multi-level paths (like /category/product), you can adjust the regex to exclude paths ending with /order:

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

// Match any path that doesn't end with /order
Route::get('/{url}', 'Controller@view')->name('view')->where('url', '^(?!.*\/order$).*$');

Critical Final Step

If you've ever run php artisan route:cache on your server (or locally), make sure to clear the cache with:

php artisan route:clear

Cached routes don't update automatically, so this is a common gotcha that causes "missing" routes even when they show up in route:list.

Test your Order Now link again, and it should route correctly to Controller@order now!

内容的提问来源于stack exchange,提问作者universal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:08:04