Laravel Passport Route [登录]未定义 [英] Laravel Passport Route [login] not defined

查看:152
本文介绍了Laravel Passport Route [登录]未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel护照进行身份验证

i use Laravel passport for auth

在api.php中

Route::get('/todos', function(){
  return 'hello';
})->middleware('auth:api');

但是当打开localhost:8000/api/todos时,我看到以下错误

but when open localhost:8000/api/todos I see the following error

 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

推荐答案

您是否直接在浏览器搜索栏中输入了上述URL? 如果您做错了方法,因为您还需要在请求中输入API令牌__ !!

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__!!

要检查请求中是否包含令牌,或者是否要制作自己的中间件.

To check either request includes token or not make your own middleware.

创建中间件的命令

php artisan make:middleware CheckApiToken

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

http://localhost:8000/api/todos?api_token = API_TOKEN_HERE

这篇关于Laravel Passport Route [登录]未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆