Laravel路由无法正常工作(404未找到)问题咨询
/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:
- Register the more specific route first: Laravel matches routes in the order they're defined, so put your
orderroute above theviewroute. - 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

