Laravel POST请求失败报MethodNotAllowedHttpException(405),已配置路由与CSRF
Hey there, let's work through why your POST request is throwing a 405 error—even with the CSRF field and route setup in place. Here are the key fixes and checks to run:
Fix the extra slash in your POST route
Looking at your route definition, there's a double slash after/assessin the POST route:Route::post('/assess//{room_name}/{subject_selected}/{unit_selected}/{outcome_select...That extra slash breaks the URL pattern matching. Change it to a single slash to align with your GET route structure:
Route::post('/assess/{room_name}/{subject_selected}/{unit_selected}/{outcome_selected}', 'AssessmentHandler@step_6_assess');Ensure parameter names match exactly
Your GET route uses{outcome_selected}as the final parameter, but your truncated POST route shows{outcome_select.... Make sure the parameter name is identical across both routes—mismatched names will prevent the POST route from being recognized by the request.Verify your form's target endpoint
Your form doesn't specify anactionattribute, so it defaults to submitting to the current page's URL (the GET route's URL). Double-check that your POST route's URL structure exactly mirrors the GET route's structure (only differing in the HTTP method) so the form submission hits the correct endpoint.Clear your Laravel route cache
If you've cached routes locally or are running in production, old cached routes might be overriding your updated configuration. Run this command to clear the cache and apply your latest route changes:php artisan route:clearValidate your registered routes
Runphp artisan route:listto view all active routes. Confirm that your POST route is listed with the correct HTTP method (POST), matching URL pattern, and linked controller method. If it's missing or has a typo, that's a clear indicator of the issue.Double-check the CSRF token (for thoroughness)
While a missing CSRF token would throw a 419 error (not 405), it's worth verifying thatcsrf_field()renders correctly. View your page's source code and look for a hidden input like:<input type="hidden" name="_token" value="YOUR_UNIQUE_CSRF_TOKEN">If it's missing, ensure your Blade template is properly rendering the directive.
Fixing the double slash and parameter name mismatch should resolve the 405 error in most cases. If not, the route cache check and route list verification will help you spot any remaining issues.
内容的提问来源于stack exchange,提问作者Sheldon Scott

