You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel POST请求失败报MethodNotAllowedHttpException(405),已配置路由与CSRF

Troubleshooting the 405 MethodNotAllowedHttpException for Your Laravel POST Form

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 /assess in 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 an action attribute, 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:clear
    
  • Validate your registered routes
    Run php artisan route:list to 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 that csrf_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:24:54