Laravel项目部署至虚拟主机后出现路由未定义与405错误求助
Fixing Laravel 5 Deployment Errors on Virtual Host
Hey there, let's tackle these two common Laravel 5 deployment issues one by one—they're pretty typical when moving from a local environment to shared hosting, so we'll get you sorted out.
1. "Route [login] not defined." Error
This error pops up when Laravel can't locate a route named login in your registered routes. Here's how to fix it:
- Check if Auth routes are registered: In
routes/web.php, make sure you have eitherAuth::routes();(for Laravel 5.2 and above) orRoute::auth();(for older 5.x versions). If you built a custom login system instead of using the default Auth scaffolding, ensure your login route has a name assigned like this:Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login'); - Clear route cache: Deployment often leaves old route caches hanging around. If you can access SSH on your host, run:
If you don't have SSH access, manually delete thephp artisan route:clearbootstrap/cache/routes.phpfile from your project—this will force Laravel to re-register all routes. - Verify route name spelling: Double-check anywhere you're calling
route('login')—even a tiny typo (likeLoginwith a capital L) can break this. While Laravel route names are case-insensitive, it's best to match the exact name you defined. - Confirm RouteServiceProvider setup: Open
app/Providers/RouteServiceProvider.phpand ensure themapWebRoutesmethod correctly loads yourroutes/web.phpfile. The default setup should look like this:protected function mapWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }
2. "405 (Method Not Allowed)" Error
This error means the HTTP method your request uses (like GET/POST/PUT) doesn't match what the route is configured to accept. Let's fix this:
- Match request method to route definition: If you're submitting a form, make sure the form's
methodattribute matches the route's method. For example, if your route is defined asRoute::post('/submit-form', 'FormController@store'), your form should have<form method="POST">. If you're using PUT/PATCH/DELETE, add the method spoofing field:<form method="POST" action="/update-item"> @csrf @method('PUT') <!-- form fields here --> </form> - Ensure CSRF protection is in place: Laravel blocks POST/PUT/DELETE requests without a valid CSRF token. For forms, add
@csrf(Laravel 5.6+) or{{ csrf_field() }}(older 5.x versions) inside your form. For AJAX requests, include the token in your request headers:$.ajax({ url: '/api/action', method: 'POST', headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' }, // other data }); - Fix your .htaccess and virtual host config: This is a super common gotcha! Make sure your virtual host's
DocumentRootpoints to the public folder of your Laravel project, not the root project folder. Also, verify yourpublic/.htaccesshas the correct rewrite rules:<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] </IfModule> - Clear route cache (again!): Just like the first error, old route caches can cause method mismatches. Run
php artisan route:clearor delete thebootstrap/cache/routes.phpfile to refresh your routes.
内容的提问来源于stack exchange,提问作者Kamal L'azz
相关产品推荐
相关产品推荐

