Laravel资源控制器show/edit路由出现404 Not Found错误求助
Hey Ahmad, let's work through those 404 errors you're seeing on your show and edit routes for the websites resource controller in your Laravel project. Based on the partial route code you shared, here are the most common fixes to check:
1. Finish Defining Your Resource Controller Route
Your code cuts off at the websites group, so my first guess is you haven't properly registered the resource controller. Inside that websites sub-group, make sure you have this line to generate all standard resource routes (including show and edit):
Route::resource('websites', 'WebsiteController');
If you only need specific resource routes instead of all, you can use the only parameter like this:
Route::resource('websites', 'WebsiteController')->only(['index', 'show', 'edit', 'update']);
2. Verify Route Parameters Are Passed Correctly
The show and edit routes require a model ID parameter (e.g., {website}). If you're generating URLs incorrectly, you'll hit a 404. Double-check:
- When using the
route()helper: Make sure you pass the model instance or ID as the parameter. Example:route('master.websites.show', ['website' => $website->id]); // Or cleaner with route model binding: route('master.websites.show', $website); - In blade templates: Ensure your links include the ID. Example:
<a href="{{ route('master.websites.edit', $website) }}">Edit Website</a>
3. Check Registered Routes with Artisan
Run this command to list all active routes in your project. Look for master.websites.show and master.websites.edit to confirm they exist, have the right URI, and point to the correct controller method:
php artisan route:list
If these routes aren't in the list, your resource controller isn't being registered properly—go back to step 1.
4. Confirm Controller Methods Exist
Make sure your WebsiteController has the show() and edit() methods defined. If you're using route model binding, the method should accept the model instance:
// Route model binding example public function show(Website $website) { return view('master.websites.show', compact('website')); } // Basic ID parameter example public function edit($id) { $website = Website::findOrFail($id); return view('master.websites.edit', compact('website')); }
If you're using findOrFail(), it will throw a 404 if the ID doesn't exist—so make sure the record you're trying to access actually exists in the database!
5. Clear Route Cache (If You've Used It)
If you ran php artisan route:cache at any point, Laravel will use a cached version of your routes. If you added the resource controller after caching, the new routes won't be loaded. Clear the cache with:
php artisan route:clear
6. Check Guard/Middleware Configuration
Your routes use the auth:master middleware. Double-check that your config/auth.php has the master guard properly configured:
'guards' => [ 'master' => [ 'driver' => 'session', 'provider' => 'masters', ], ], 'providers' => [ 'masters' => [ 'driver' => 'eloquent', 'model' => App\Models\Master::class, ], ],
While an incorrect guard usually redirects to login instead of 404, it's worth ruling out if all other checks pass.
内容的提问来源于stack exchange,提问作者Ahmad Badpey




