sevenTopo's avatar

Laravel Passport .Route [login] not found

Hi Everyone.

When i try to access to any route in my Api.php file using Postman im facing this error ' Route [login] not defined.in file ....\Illuminate\Routing\UrlGenerator.php on line 420. so before anything i have to notice that im using Laravel Passport package ,following the Laravel Documentation https://laravel.com/docs/6.x/passport.

this is My Api.php file


Route::post('login', 'UserController@login');
Route::post('register', 'UserController@register');

Route::group(['middleware' => 'auth:api'], function(){
        Route::resource('/task', 'TaskController');
        Route::resource('/category', 'CategoryController');
        Route::get('/category/{category}/tasks', 'CategoryController@tasks');
    });

and this is on of my controllers TaskController store function for exemple.


/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $task = Task::create([
            'name' => $request->name,
            'category_id' => $request->category_id,
            'user_id' => $request->user_id,
            'order' => $request->order
        ]);

        $data = [
            'data' => $task,
            'status' => (bool) $task,
            'message' => $task ? 'Task Created!' : 'Error Creating Task',
        ];

        return response()->json($data);
    }

And for Laravel Passport Configuration : i have my AuthServiceProvider with the necessary imports

 /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
   public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }

config/auth.php

 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
    ],

i did what it should be done so what is the problem guys . thanks for taking the time and effort before anything .

0 likes
1 reply
ahmeddabak's avatar

you need to give the route a name to access it using the name route('login') if you are trying to access it using the url, then you have to write api/login since you place your routes in the api.php file, it will always be prefixed with api


Route::post('login', 'UserController@login')->name('login');
Route::post('register', 'UserController@register')->name('register');

Please or to participate in this conversation.