我阅读了很多关于 api 路由的新概念的帖子。我了解 api 路由用于移动平台,但它们之间是否存在代码级别差异
在 RouteServiceProvider
我可以看到
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
根据此网络路线使用
These routes all receive session state, CSRF protection, etc.
api路由
These routes are typically stateless.
我的问题是
stateless
在 api route ? session state
, CSRF protection
.这是否意味着 api 路由没有使用 session 状态、CSRF 保护? web
和 api
路由,有什么好处吗? 最佳答案
stateless
在 api route ? 这意味着服务器不会在请求之间保存客户端“状态”。这是关于 REST What exactly is RESTful programming? 的几句话
session state
, CSRF protection
.这是否意味着 api 路由没有使用 session 状态、CSRF 保护? 一切皆有可能,但不是必需的。您仍然可以使用 session 等,但这是违反 REST 原则的。
web
和 api
路由,有什么优势这只是为了您的方便。在 Laravel 5.2 中,您需要为 ['web'] 或 ['api'] 等路由指定中间件,但不再需要它。在 5.3 中,路由存储在单独的文件中,并且不需要指定路由中间件。
关于php - laravel 5.3 中 web 路由和 api 路由的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121445/