Laravel 5.5 出现404页面未找到错误,新手求助排查
Hey there, let's work through this 404 issue step by step! Based on the code snippets you shared, here are the most likely fixes to check:
检查路由冲突与访问URL
You've defined both a standaloneGET /curriculum-sectionsroute and aRoute::resourcefor the same path. The resource route automatically generates anindexmethod route (which duplicates your standalone GET route), but more crucially, theshowmethod from the resource route maps to the URLGET /curriculum-sections/{curriculum_section}(note the snake_case parameter). If you're trying to access the show page, make sure you're visiting a URL likeyour-domain/curriculum-sections/1(replace1with a valid ID from yourcurriculumsectionstable).隐式模型绑定匹配问题
In your controller'sshowmethod, you're using implicit model binding withCurriculumsection $curriculumsection. Laravel 5.5's implicit binding relies on the route parameter name matching the variable name (or the model's route key). Since your resource route generates the parameter ascurriculum_section(snake_case), but your variable is$curriculumsection(camelCase), this mismatch can cause Laravel to fail locating the model, resulting in a 404. You can fix this in two ways:- Update your controller method parameter to match the route parameter:
public function show(Curriculumsection $curriculum_section) { // Your logic here } - Or, define a custom route key name in your
Curriculumsectionmodel:public function getRouteKeyName() { return 'id'; // Or whatever your primary key column name is }
- Update your controller method parameter to match the route parameter:
清理路由缓存
If you've runphp artisan route:cacheat any point, Laravel will use cached routes instead of your updatedroutes/web.phpfile. Run this command to clear the cache and load your latest route configuration:php artisan route:clear验证控制器命名空间
Ensure yourCurriculumsectionsControlleris in the defaultApp\Http\Controllersnamespace. If it's placed in a subdirectory (likeApp\Http\Controllers\Admin), you need to specify the full namespace in your route:Route::resource('/curriculum-sections', 'Admin\CurriculumsectionsController');检查模型与数据库一致性
Double-check that the ID you're using in the URL actually exists in yourcurriculumsectionsdatabase table. If the record doesn't exist, Laravel's implicit binding will automatically return a 404, even if your route configuration is correct.
内容的提问来源于stack exchange,提问作者kya

