我正在尝试使用Laravel Passport来响应 api 中的令牌
我关注了这个博客来获取我的代码: https ://blog.logrocket.com/laravel-passport-a-tutorial-and-example-build/
我收到此错误:
Illuminate\Contracts\Container\BindingResolutionException:目标类 [Auth\UserAuthController]
正如 ulitmator 建议我添加的那样 x-www-form-urlencoded,但我仍然得到这个 Laravel 屏幕作为响应。
看起来它正在寻找文字类:Auth\\UserAuthController。这不是完整的命名空间。您正在寻找它:App\\Http\\Controllers\\Auth\\UserAuthController。这看起来像是路由文件配置方式的问题。请参阅https://laravel.com/docs/9.x/routing和https://laravel.com/docs/9.x/controllers或您使用的 laravel 版本。做事有不同的方式。
我通常通过指定完整的命名空间来组织我的路线,如下所示:
\nuse App\\Http\\Controllers\\Auth\\UserAuthController;\nuse App\\Http\\Controllers\\EmployeeController;\n\n// ...\n\nRoute::post(\'/register\', [UserAuthController::class, \'register\']);\nRoute::post(\'/login\', [UserAuthController::class, \'login\']);\n\nRoute::apiResource(\'/employee\', EmployeeController::class)->middleware(\'auth:api\');\nRun Code Online (Sandbox Code Playgroud)\n另请参阅https://litvinjuan.medium.com/how-to-fix-target-class-does-not-exist-in-laravel-8-f9e28b79f8b4
\n在 Laravel 7 之前,RouteServiceProvider.php 文件具有以下代码:
\nprotected $namespace = \'App\\Http\\Controllers\';\nRoute::middleware(\'web\')\n ->namespace($this->namespace)\n ->group(base_path(\'routes/web.php\'));\nRun Code Online (Sandbox Code Playgroud)\n它的作用是告诉 Laravel 使用 Web 中间件和 App\\Http\\Controllers 命名空间加载 paths/web.php 中的路由。反过来,这意味着每当您使用字符串语法声明路由时,Laravel 都会在 App\\Http\\Controllers 文件夹中查找该控制器。
\n在 Laravel 8 中,删除了 $namespace 变量,并将 Route 声明更改为:
\nRoute::middleware(\'web\')\n ->group(base_path(\'routes/web.php\'));\nRun Code Online (Sandbox Code Playgroud)\n这意味着从 Laravel 8 开始,当您使用字符串语法声明路由时,Laravel 不会\xe2\x80\x99t 会在 App\\Http\\Controllers 中查找控制器。
\n虽然我不推荐这样做,但您也可以通过在 RoutesServiceProvider.php 文件中添加 3 行代码来手动添加命名空间,使其表现得像旧版 Laravel 一样:
\nprotected $namespace = \'App\\Http\\Controllers\'; // Add this line\n\n\npublic function boot() {\n $this->configureRateLimiting();\n\n $this->routes(function() {\n Route::middleware(\'web\')\n ->namespace($this->namespace) // Add this line\n ->group(base_path(\'routes/web.php\'));\n\n Route::prefix(\'api\')\n ->middleware(\'api\')\n ->namespace($this->namespace) // Add this line\n ->group(base_path(\'routes/api.php\'));\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n但通常 Laravel 8+ 的处理方式是在路由文件中使用useand ::class,并为控制器指定完整的命名空间。看起来您正在遵循为较旧的 Laravel 构建的教程。如果使用 laravel 8+,请改用 laravel 8+ 的方式组织路由文件。
编辑:
\n在 Postman 中,您可以向以下地址发帖:
\nhttp://127.0.0.1:8000/api/register\nRun Code Online (Sandbox Code Playgroud)\n并指定字段:
\n名称、电子邮件、密码和密码确认。
\n(请注意,您提供的屏幕截图中没有显示密码和密码确认,因此验证失败)
\n确保电子邮件使用有效的电子邮件地址语法。
\n确保密码和password_confirmation 匹配。
\n确保您按照教程使用 x-www-form-urlencoded。如果您愿意,您可以使用表单数据,但是 Content-Type 必须为空,以便邮递员可以为您填充多部分/表单数据边界。
对于名称,我使用:a
\n对于电子邮件,我使用:a@b.com
\n对于密码,我使用:a
\n对于密码确认,我使用:a
如果遇到验证错误,您还可以对此进行调试:
\n调试选项 1:编辑 UserAuthController.php 文件,\n而不是使用:
\nuse App\\Http\\Controllers\\Auth\\UserAuthController;\nuse App\\Http\\Controllers\\EmployeeController;\n\n// ...\n\nRoute::post(\'/register\', [UserAuthController::class, \'register\']);\nRoute::post(\'/login\', [UserAuthController::class, \'login\']);\n\nRoute::apiResource(\'/employee\', EmployeeController::class)->middleware(\'auth:api\');\nRun Code Online (Sandbox Code Playgroud)\n使用 try-catch 代替,如下所示:
\nprotected $namespace = \'App\\Http\\Controllers\';\nRoute::middleware(\'web\')\n ->namespace($this->namespace)\n ->group(base_path(\'routes/web.php\'));\nRun Code Online (Sandbox Code Playgroud)\n调试选项 2:您还可以在邮递员请求中指定接受标头,而不是使用 try-catch。通常,当 Laravel 中的验证失败时,它会将您重定向到上一页并将错误发送到那里。如果不显示它们,您只会看到 laravel 欢迎页面/。但如果你Accept为 指定一个标头application/json,laravel 会理解你正在寻找 json 响应。在邮递员中,您的标题将如下所示:\n
另请注意,如果您要发送表单数据,则不得设置 Content-Type 标头。请务必取消选中它,这样您就不会发送它。如果你选择x-www-form-urlencoded,它会自动设置一个Content-Type headerapplication/x-www-form-urlencoded。如果您要使用 x-www-form-urlencoded,这很好。但如果您选择表单数据,这将是不匹配的,您将遇到问题。对于表单数据,由于您无法指定边界并用边界分隔字段,因此应将 Content-Type 留空。这将允许邮递员自动为您设置边界。
假设它有效,您将在用户表中看到一个条目:
\n\n| 归档时间: |
|
| 查看次数: |
444 次 |
| 最近记录: |