Laravel路由:api.php与web.php的区别详解
Hey there! Great question—this is a super common point of confusion when you're getting started with Laravel, especially if you're building a mix of traditional web apps and APIs (like with Vue.js, which you mentioned). Let me break down the key differences between routes/web.php and routes/api.php for you:
1. Middleware Stack
web.phpuses thewebmiddleware group by default, which includes tools for stateful web apps:- Session handling (
StartSession) to maintain user login state across requests - CSRF protection (
VerifyCsrfToken) to block cross-site request forgery attacks - Cookie encryption (
EncryptCookies) for secure user cookie storage - Helpers like
ShareErrorsFromSessionto pass validation errors to Blade views
- Session handling (
api.phpuses the leanerapimiddleware group, built for stateless APIs:throttle:apito rate-limit requests and prevent abuseBindRequestsfor automatic route model binding- No session or CSRF protection—APIs rely on tokens (like Laravel Sanctum or JWT) instead of cookies/sessions for auth
2. Route Prefixing
- Routes in
web.phplive at your app's root domain, e.g.,https://your-app.com/dashboard - All
api.phproutes automatically get an/apiprefix (configurable inRouteServiceProvider.php), so a route likeRoute::get('/users', ...)becomeshttps://your-app.com/api/users
3. Stateful vs. Stateless Behavior
web.phproutes are stateful: they use sessions and cookies to remember authenticated users. Perfect for traditional server-rendered Blade pages where you log in once and stay logged in across navigation.api.phproutes are stateless: every request needs to include auth credentials (like a token in theAuthorizationheader) because there's no persistent session. This is ideal for your Vue.js frontend, which will make AJAX calls and manage auth tokens client-side.
4. Default Controller Namespaces
web.phploads controllers from theApp\Http\Controllersnamespace by default (so you can writeDashboardControllerinstead of the full namespace path)api.phpdefaults to theApp\Http\Controllers\Apinamespace—you can adjust this inRouteServiceProviderif you prefer a different folder structure.
5. Real-World Use Cases
- Use
web.phpfor:- Server-rendered pages (login screens, Blade-based admin dashboards)
- Form submission routes that require session-based auth
- Use
api.phpfor:- All endpoints your Vue.js app calls (fetching data, creating/updating records)
- Third-party integrations that need programmatic access to your app's data
Quick Example
For a Vue component fetching product data:
// routes/api.php Route::get('/products', [ProductController::class, 'index'])->middleware('auth:sanctum');
Your Vue app would hit GET /api/products with a Sanctum token in the request headers.
For a server-rendered admin dashboard:
// routes/web.php Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->middleware('auth');
This uses session-based auth to check if the user is logged in before loading the page.
Hope that clears things up! Let me know if you have follow-up questions about setting up auth for your Vue app or structuring your routes further.
内容的提问来源于stack exchange,提问作者Sam

