laravel 5.3 中的 Web 路由和 api 路由之间的区别

Diff between web routing and api routing in laravel 5.3

我读了很多 post 关于 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.

我的问题是

  1. api路由中的stateless是什么意思?

  2. Web 路由使用 session stateCSRF protection。这是否意味着 api 路由未使用会话状态、CSRF 保护?

  3. Laravel 5.3 使用单独的 webapi 路由,有什么优势吗?

  1. api 路线中的 stateless 是什么意思?

这意味着服务器不会在请求之间保存客户端 'state'。这是关于 REST 的几句话 What exactly is RESTful programming?

  1. Web 路由使用 session stateCSRF protection。这是否意味着 api 路由未使用会话状态、CSRF 保护?

一切皆有可能,但不是必需的。您仍然可以使用会话等,但这是违反 REST 原则的。

  1. Laravel 5.3使用单独的webapi路由,有什么优势吗

这只是为了您的方便。在 Laravel 5.2 中,您需要为 ['web'] 或 ['api'] 等路由指定中间件,但不再需要了。在 5.3 中,路由存储在单独的文件中,不需要指定路由中间件。