404 - 未找到护照 Api 存在的路线 Laravel 8
404 - Not Found Passport Api Route That Exists Laravel 8
我安装了 passport 并在 api.php 文件中有一个路由。网络路由有效,但 api 路由总是给我一个 404 - Not Found in Postman。
This is my route
This is my UserController Function
我坚持了一个星期。可能有很多原因会导致此问题。
1 - Make sure you installed passport correctly.
似乎 laravel 8 带有“Sanctum” pre-installed 因此您可能会在某些 passport 的安装要求上出错。再次完成安装。 Passport Installation 看看我下面的例子可能的错误。
查看您的用户模型并确保您有“使用 Laravel\Passport\HasApiTokens;”不是“使用 Laravel\Sanctum\HasApiTokens;”
2- If you are not using "Sanctum", you are better off removing it from your project
前往VSCODE左侧搜索栏,搜索“Sanctum”。
单击“composer.json”,转到要求部分并小心删除“Sanctum”要求。还要检查所有其他搜索结果,以确保您没有在应该是 passport 的地方使用 Sanctum。如果需要,请删除它们。
3- In your terminal, type the command below to Update composer and generate a new lock file
作曲家更新
4- If you moved your User model to a new folder (eg: version1) like I did, go to "config/auth.php" and update the providers for user with the correct path of the User model
5- Make sure in the api section of app/Http/Kernel.php, "\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class" is commented out.
6- Go to terminal, clear your routes from cache, list them with the commands below and check if the route you listed in api.php shows there.
php artisan route:clear
php artisan route:list
如果“php artisan route:list”显示错误,请确保您的路线声明正确。为清楚起见,请确保使用控制器的完整路径并交叉检查 UserController 以确保函数与 api.php 文件中的内容完全匹配(区分大小写)。此外,如果您在路线中使用控制器,请在“use App\Http\Controllers\version1\UserController;”上方声明它。如下图所示。在终端中重复“php artisan route:clear”和“php artisan route:list”以确保没有错误并且列出了您的路线.
7- Make sure the url you are calling in postman is correct and the method is also correct.
记住,如果你在 api.php 中的 url 是“/v1/user/register”,那么在 postman 中,你应该有“api/v1/user/register" 在 post 人中。这意味着您的最终 url 将是“http://myvhostname.local/api/v1/user/register”
记住,如果您在 api.php 中声明您的路线是“get”,那么请确保它在 postman 中是“get”。如果是 post,请确保它们在 postman 中也匹配。
转到 postman 的“headers”部分并包括“Accept=application/json”,如下所示
8- If you still have a 404-Not Found, open your vhost file "httpd-vhosts.conf" in "Applications >> XAMPP >> xamppfiles >> etc >> extra" and make sure your vhost is declared correctly as below. Improper declaration can make web routes work but api routes won't. Pay close to "Allowoverride All".
Your routes should work now. If this worked for you, an upvote would be appreciated
只是一个快速的。如果错误代码是 404,请仔细检查您是在 Postman 中发出 POST 请求,而不是 GET。
请按 php artisan route:list
检查路线。还要检查你的 APP_URL in.ENV 文件
此外,您还应该检查 /config/app.php
和对 App\Providers\RouteServiceProvider::class
的引用(如果提供商缺少)。
将 RouteServiceProvider 添加到 /config/app.php
后,路由将再次工作。
对于文件的原始 Laraval 版本,请在此处查看:https://github.com/laravel/laravel/blob/a6c68c24c9938beef0128c3288502b8fbdf8e93d/config/app.php#L178
我安装了 passport 并在 api.php 文件中有一个路由。网络路由有效,但 api 路由总是给我一个 404 - Not Found in Postman。
This is my route
This is my UserController Function
我坚持了一个星期。可能有很多原因会导致此问题。
1 - Make sure you installed passport correctly.
似乎 laravel 8 带有“Sanctum” pre-installed 因此您可能会在某些 passport 的安装要求上出错。再次完成安装。 Passport Installation 看看我下面的例子可能的错误。
查看您的用户模型并确保您有“使用 Laravel\Passport\HasApiTokens;”不是“使用 Laravel\Sanctum\HasApiTokens;”
2- If you are not using "Sanctum", you are better off removing it from your project
前往VSCODE左侧搜索栏,搜索“Sanctum”。
单击“composer.json”,转到要求部分并小心删除“Sanctum”要求。还要检查所有其他搜索结果,以确保您没有在应该是 passport 的地方使用 Sanctum。如果需要,请删除它们。
3- In your terminal, type the command below to Update composer and generate a new lock file
作曲家更新
4- If you moved your User model to a new folder (eg: version1) like I did, go to "config/auth.php" and update the providers for user with the correct path of the User model
5- Make sure in the api section of app/Http/Kernel.php, "\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class" is commented out.
6- Go to terminal, clear your routes from cache, list them with the commands below and check if the route you listed in api.php shows there.
php artisan route:clear
php artisan route:list
如果“php artisan route:list”显示错误,请确保您的路线声明正确。为清楚起见,请确保使用控制器的完整路径并交叉检查 UserController 以确保函数与 api.php 文件中的内容完全匹配(区分大小写)。此外,如果您在路线中使用控制器,请在“use App\Http\Controllers\version1\UserController;”上方声明它。如下图所示。在终端中重复“php artisan route:clear”和“php artisan route:list”以确保没有错误并且列出了您的路线.
7- Make sure the url you are calling in postman is correct and the method is also correct.
记住,如果你在 api.php 中的 url 是“/v1/user/register”,那么在 postman 中,你应该有“api/v1/user/register" 在 post 人中。这意味着您的最终 url 将是“http://myvhostname.local/api/v1/user/register”
记住,如果您在 api.php 中声明您的路线是“get”,那么请确保它在 postman 中是“get”。如果是 post,请确保它们在 postman 中也匹配。
转到 postman 的“headers”部分并包括“Accept=application/json”,如下所示
8- If you still have a 404-Not Found, open your vhost file "httpd-vhosts.conf" in "Applications >> XAMPP >> xamppfiles >> etc >> extra" and make sure your vhost is declared correctly as below. Improper declaration can make web routes work but api routes won't. Pay close to "Allowoverride All".
Your routes should work now. If this worked for you, an upvote would be appreciated
只是一个快速的。如果错误代码是 404,请仔细检查您是在 Postman 中发出 POST 请求,而不是 GET。
请按 php artisan route:list
检查路线。还要检查你的 APP_URL in.ENV 文件
此外,您还应该检查 /config/app.php
和对 App\Providers\RouteServiceProvider::class
的引用(如果提供商缺少)。
将 RouteServiceProvider 添加到 /config/app.php
后,路由将再次工作。
对于文件的原始 Laraval 版本,请在此处查看:https://github.com/laravel/laravel/blob/a6c68c24c9938beef0128c3288502b8fbdf8e93d/config/app.php#L178