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

Laravel本地环境配置正确路由仍出现POST请求404错误

Hey, let's troubleshoot why your POST request to /categories is throwing a 404 even though the route is set up in web.php. Here are the most common fixes to try:

1. Make Sure Your Form Includes the CSRF Token

Laravel enforces CSRF protection for all POST requests, and missing this can sometimes lead to unexpected 404s (or 419 errors, but in some cases it manifests as 404). Add the @csrf directive inside your form:

<form method="POST" action="/categories">
    @csrf
    <!-- Your form fields here -->
    <button type="submit">Submit</button>
</form>

For better reliability, use a named route instead of hardcoding the URL. First update your route in web.php:

Route::post('categories',"CategoryController@store")->name('categories.store');

Then reference it in your form:

<form method="POST" action="{{ route('categories.store') }}">
    @csrf
    <!-- ... -->
</form>

2. Clear and Check Your Route Cache

If you've ever run php artisan route:cache, any new changes to web.php won't take effect until you clear the cache. Run these commands:

php artisan route:clear
# Optional: Re-cache routes only if you need it later
php artisan route:cache

Also, verify your route is actually registered by running php artisan route:list—look for the POST /categories entry. If it's missing, double-check your web.php syntax (like typos in controller/method names or missing semicolons).

3. Verify Your Form's Action URL

Ensure the form's action points to the correct URL. If your app runs in a subdirectory (e.g., http://localhost/my-laravel-app), the action should be /my-laravel-app/categories instead of just /categories. Using named routes avoids this headache entirely, since Laravel handles URL generation automatically.

4. Check Your Web Server Configuration

  • Apache: Make sure the .htaccess file in your public directory exists and uses Laravel's default rewrite rules. This file ensures all requests are routed through index.php—without it, POST requests might not reach Laravel's router.
  • Nginx: Confirm your server block includes this location rule to forward requests to Laravel:
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

A missing or incorrect rule here can cause Nginx to return a 404 instead of passing the request to Laravel.

5. Confirm the store Method Exists in Your Controller

Double-check that your CategoryController has a properly defined store method, and that the controller is in the correct namespace (App\Http\Controllers):

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function create()
    {
        // Your create logic here
    }

    public function store(Request $request)
    {
        // Your store logic here
    }
}

A missing store method will prevent Laravel from resolving the route, leading to a 404.

6. Check for Route Conflicts

Routes are processed in the order they're defined in web.php. If you have a wildcard route like Route::get('categories/{id}', ...) placed before your POST /categories route, it might catch the request first. Move more specific routes (like your POST route) above wildcard routes to avoid conflicts.

If none of these fix the issue, check your Laravel log file (usually in storage/logs/laravel.log)—it might have more detailed error messages that aren't visible in the 404 response.

内容的提问来源于stack exchange,提问作者Adarsh Sojitra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:37:26