Search code examples
phplaravellaravel-passportlaravel-9

routesAreCached() method in Laravel undefined


Please help me out a bit. I'm trying to setup passport for my Laravel app by following the official docs. But i'm stuck at the step where I need to check before calling Passport::routes(). My vscode displayed an error saying

undefined method: routesAreCached()

even though when I traced back to the base abstract class ServiceProvider.php, the code there seems to be calling $this->app->routesAreCached() without any problems. Below is my App\Providers\AuthServiceProvider.php code.

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

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

        /**
         * This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:
         * 
         */

         if (! $this->app->routesAreCached()) {   // error at this line
            Passport::routes();
         }

    }
}

Solution

  • Passport's routes have been moved to a dedicated route file. You can remove the Passport::routes() call from your application's service provider. This link may help