Laravel项目页面跳转404错误:配置路由视图后仍无法访问求解决
Hey there, let's work through this 404 issue step by step—here are the most common fixes to check:
Double-check your view file path and name
Make sureaddnewemployee.blade.phpis directly in theresources/views/folder (not tucked away in a subfolder likeviews/employees/). If it is in a subfolder, you'll need to update your route to use dot notation, likeview('employees.addnewemployee'). Also, watch out for case sensitivity—on Linux/macOS,AddNewEmployee.blade.phpis not the same asaddnewemployee.blade.php, so the name has to match exactly.Verify your route is registered
Open your terminal and runphp artisan route:list—look for aGET /addnewemployeeentry in the output. If it's not there, your route isn't being loaded. If you added the route to a custom file instead ofroutes/web.php, make sure yourRouteServiceProvideris set up to load that file.Fix your URL generation (use named routes for reliability)
Your current link uses{{url('/addnewemployee')}}—first, check the page source to confirm the generated URL is correct (no typos or extra slashes). A better approach is to use named routes, which are less prone to errors:// Update your route in web.php to add a name Route::get('/addnewemployee', function() { return view('addnewemployee'); })->name('add-new-employee');Then update your link to:
<a class="btn cus-button" href="{{route('add-new-employee')}}"> Add New </a>Clear your route cache
If you've ever runphp artisan route:cachein the past, your new route won't be picked up until you clear the cache. Run this command in your terminal:php artisan route:clearCheck your server configuration
- For Apache: Ensure the
public/.htaccessfile exists and has the default Laravel rewrite rules (it should redirect all requests toindex.php). If it's missing, you can copy the default one from the Laravel base project. - For Nginx: Make sure your site config includes the correct
try_filesrule to handle Laravel's pretty URLs:location / { try_files $uri $uri/ /index.php?$query_string; }
Also, confirm your web server is pointing to the
publicfolder of your Laravel project, not the root project folder—this is a common mistake that breaks routing.- For Apache: Ensure the
Look for route conflicts
If you have a catch-all route (likeRoute::get('/{slug}', ...)) defined before your/addnewemployeeroute, it will take precedence and treataddnewemployeeas a slug parameter. Move your specific/addnewemployeeroute above any wildcard routes inweb.php.
内容的提问来源于stack exchange,提问作者Sharmila

