Laravel Passport Route [登录] 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49294253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Passport Route [login] not defined
提问by MJ7
i use Laravel passport for auth
我使用 Laravel 护照进行身份验证
in route api.php
在路由 api.php
Route::get('/todos', function(){
return 'hello';
})->middleware('auth:api');
but when open localhost:8000/api/todos I see the following error
但是当打开 localhost:8000/api/todos 我看到以下错误
InvalidArgumentException
Route [login] not defined.
* @return string
*
* @throws \InvalidArgumentException
*/
public function route($name, $parameters = [], $absolute = true)
{
if (! is_null($route = $this->routes->getByName($name))) {
return $this->toRoute($route, $parameters, $absolute);
}
throw new InvalidArgumentException("Route [{$name}] not defined.");
}
/**
* Get the URL for a given route instance.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \Illuminate\Routing\Exceptions\UrlGenerationException
*/
protected function toRoute($route, $parameters, $absolute)
{
return $this->routeUrl()->to(
$route, $this->formatParameters($p
I want if the user was not authenticated Do not redirect to any page and only see the page
我想如果用户未通过身份验证不要重定向到任何页面,只看到该页面
采纳答案by Ramzan Mahmood
Did you enter above-mentioned URL directly in browser search bar? If you did its wrong way because you also need to enter API token with your request__!!
您是在浏览器搜索栏中直接输入上述网址吗?如果你做错了,因为你还需要在你的请求中输入 API 令牌__!!
To check either request includes token or not make your own middleware.
检查任一请求是否包含令牌或不制作您自己的中间件。
Command to create Middleware
创建中间件的命令
php artisan make:middleware CheckApiToken
https://laravel.com/docs/5.6/middleware
https://laravel.com/docs/5.6/middleware
change middleware handle method to
将中间件句柄方法更改为
public function handle($request, Closure $next)
{
if(!empty(trim($request->input('api_token')))){
$is_exists = User::where('id' , Auth::guard('api')->id())->exists();
if($is_exists){
return $next($request);
}
}
return response()->json('Invalid Token', 401);
}
Like This Your Url should be like this
这样你的网址应该是这样的
回答by Eki
Use Postman and set the Header Accept: application/json
otherwise Laravel Passport would never know it's an API client and thus redirect to a /login page for the web.
使用 Postman 并设置 HeaderAccept: application/json
否则 Laravel Passport 永远不会知道它是一个 API 客户端,从而重定向到网络的 /login 页面。
回答by Fazil Raza
You also have to add another header Key: Accept and value: application/json
您还必须添加另一个标头 Key: Accept 和 value: application/json
回答by Ayman Elshehawy
Check Your Header Request to put
检查您的标头请求以放置
Authorization = Bearer {your token}
授权 = Bearer {您的令牌}
回答by Amin Shojaei
In the following of @Ekianswer,
在@Eki的以下回答中,
This error is because you didn't set "Accept" field in your headers.
此错误是因为您没有在标题中设置“接受”字段。
To avoid this error, add a middleware with priority to Authenticate to check that:
为避免此错误,请添加一个优先级为 Authenticate 的中间件以检查:
add an extra middleware with below handler
public function handle($request, Closure $next) { if(!in_array($request->headers->get('accept'), ['application/json', 'Application/Json'])) return response()->json(['message' => 'Unauthenticated.'], 401); return $next($request); }
set priority in app/Http/Kernel.php
protected $middlewarePriority = [ ... \App\Http\Middleware\MyMiddleware::class, // new middleware \App\Http\Middleware\Authenticate::class, ... ];
add new middleware to your route
Route::get('/todos', function(){ return 'hello'; })->middleware('MyMiddleware', 'auth:api');
添加具有以下处理程序的额外中间件
public function handle($request, Closure $next) { if(!in_array($request->headers->get('accept'), ['application/json', 'Application/Json'])) return response()->json(['message' => 'Unauthenticated.'], 401); return $next($request); }
在 app/Http/Kernel.php 中设置优先级
protected $middlewarePriority = [ ... \App\Http\Middleware\MyMiddleware::class, // new middleware \App\Http\Middleware\Authenticate::class, ... ];
向您的路线添加新的中间件
Route::get('/todos', function(){ return 'hello'; })->middleware('MyMiddleware', 'auth:api');
回答by Designer Coder
You also have to add another header Key: X-Requested-With and value: XMLHttpRequest
您还必须添加另一个标头 Key:X-Requested-With 和 value:XMLHttpRequest