简体   繁体   English

如何在自定义 laravel 包中访问 Sanctum 包

[英]How to access Sanctum package in custom laravel package

我想访问在项目路由中运行良好的 laravel sanctum auth 我正在制作一个自定义的 api 包,它需要在自定义包路由中使用相同的 sanctum 身份验证

use auth sanctum middleware for your routes, See below example.为您的路线使用 auth sanctum 中间件,请参见下面的示例。

https://laravel.com/docs/9.x/sanctum#protecting-routes https://laravel.com/docs/9.x/sanctum#protecting-routes

I was having the same problem, but I found that the packet routes did not have a default guard and the session was not accessible through the packet.我遇到了同样的问题,但我发现数据包路由没有默认保护,并且无法通过数据包访问会话。

The solution was to add the 'web' middleware to the routes.解决方案是将“网络”中间件添加到路由中。


Before:前:

Route::get('/dashboard', [HomeController::class, 'index'])->middleware(['auth:sanctum'])->name('dashboard');

After:后:

Route::get('/dashboard', [HomeController::class, 'index'])->middleware(['web', 'auth:sanctum'])->name('dashboard');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM