Search code examples
phplaravellaravel-5laravel-5.3laravel-authorization

Why I can't find the "App\Extensions\RiakUserProvider" namespace in my Laravel project following this "Adding a custom provider" tutorial?


I am absolutely new to PHP and Laravel.

I am working on a Laravel 5.3 application and I have to use a custom web service to check the user credential so I am trying to follow this official tutorial about Adding a custom provider to handle user access: https://laravel.com/docs/5.3/authentication#adding-custom-user-providers. So, in theory it seems pretty simple but I am finding some difficulties.

As you can see in the previous tutorial as first step it modify the App\Providers\AuthServiceProvider class contained into the Laravel project.

So, I have modified my AuthServiceProvider according to the tutorial example obtaining this:

<?php

namespace App\Providers;

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

use Illuminate\Support\Facades\Auth;
use App\Extensions\RiakUserProvider;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * 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();

        // CUSTOM CODE:
        Auth::provider('riak', function ($app, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\UserProvider...

            return new RiakUserProvider($app->make('riak.connection'));
        });
    }
}

The problem is that it can't find the App\Extension namespace, this:

use App\Extensions\RiakUserProvider;

PhpStorm signs Exstensions in red saying "Undefined Extensions namespace" so it can't use the RiakUserProvider class in my code.

Why? Do I have to add some dependencies in Composer? What is wrong? What am I missing? How can I fix this issue?

What exactly is the RiakUserProvider class?

What exactly does this code:

Auth::provider('riak', function ($app, array $config) {
    // Return an instance of Illuminate\Contracts\Auth\UserProvider...

    return new RiakUserProvider($app->make('riak.connection'));
});

Solution

  • In the Laravel docs the RiakUserProvider class is just an example custom User Provider. Class is located in App\Extensions namespace, but the actual provider class content was not provided.

    If you want to create a custom User Provider you should create a folder named Extensions in your App folder and create RiakUserProvider.php file containing RiakUserProvider class. This follows PSR-4 class autoloading standard.

    When you create your own User Provider please make sure it implements Illuminate\Contracts\Auth\UserProvider interface.

    Here is a nice step by step tutorial of creating one:

    https://www.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers