laravel route api.php 与 web.php 的区别

更新日期: 2019-03-28 阅读次数: 10545 字数: 167 分类: Laravel

  • 如果是一个纯粹的网站项目,只需要使用 web.php 里的路由就可以了。ajax 需要使用到的 route 也是定义在 web.php 中。
  • 如果是一个非网页项目,例如微信小程序,Android / iOS APP 项目,或者给三方开发者提供接口,则应该使用 api.php。因为 cookie 那一套并不适用。

Web 路由使用了 web middlewareGroups 。包含类似 Session 及 CSRF 保护,而网页 ajax 同意需要 CSRF 保护。

API 路由使用了 api middlewareGroups

参考 app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:120000,1',
        'bindings',
    ],
];

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式