【问题标题】:Not working api route in laravel 5.6 with postman在 laravel 5.6 中无法使用邮递员使用 api 路由
【发布时间】:2018-12-26 11:42:28
【问题描述】:

我在 laravel 中测试护照时遇到问题,当 postman 中的 testo 给我一个错误时。

"message": "",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
    "file": "/data/www/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
    "line": 179,

路由/api.php

Route::group([
    'prefix'        => 'api',
    'middleware'    => ['auth.api']
], function () {
    Route::post('details', 'API\UserController@details');
});

用户控制器.php

    public function details()
{
    dd("Ijdsijds");
    $user = Auth::user();
    return response()->json(['success' => $user], $this->successStatus);
}

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:60,1',
            'bindings',
            'cors' => [
                \Barryvdh\Cors\HandleCors::class,
            ]
        ],
    ];

验证CRFSToken.php

protected $except = [
        //'authenticate'
        '/api/*'
    ];

我哪里错了?路线在 route: list 中列出

【问题讨论】:

  • 你的邮递员发送到什么路线?
  • 你已经放置了一个中间件'middleware' => ['auth.api'],它有什么作用?您是否随请求一起发送任何 API 密钥(例如)?
  • 您不应该真正将 dd 命令与邮递员一起使用,它是专为浏览器设计的。只需返回一个 json 或字符串。在已经设置好的 api.php 上编写路由时不需要 Api 前缀。这可能是你的问题。

标签: php laravel api postman


【解决方案1】:

当您使用routes/api.php 时,它是无状态的,因此您不会获得任何会话值(此处为Auth)。

要获得价值,请使用Passport Helpful link

或使用JWT

【讨论】:

  • 我已经在使用护照,我正在通过邮递员测试 api,它返回了这个错误,我已经在这里尝试了一切
猜你喜欢
  • 2018-09-07
  • 2022-11-03
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 2021-02-17
  • 2020-01-24
  • 2019-02-19
相关资源
最近更新 更多