Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Moggz's avatar
Level 3

How to register Service Provider

I'm following the Laravel from Scratch tutorials.

In Episode 22 "Core Concepts: Service Providers", he creates a Service Provider called SocialServiceProvider. When he reloads the page he gets an "Unresolvable dependency resolving" error.

He then registers the new SocialServiceProvider in config/app.php, then refresh the page and now it works.

After I register the SocialServiceProvider in config/app.php I still get a "Unresolvable dependency resolving" error, and the register method for the new Service Provider is not hit.

In the tutorials it says that all of the Service Providers registered in config/app.php will be loaded, so why does this not work?

0 likes
14 replies
Moggz's avatar
Level 3

I used artisan make:provider SocialServiceProvider to create the new service provider.

In the autoloaded service providers section of config/app.php I have added the line...

    App\Providers\SocialServiceProvider::class,

The register function of SocialServiceProvider is just...

public function register()
    {
        dd("hello");
        $this->app->singleton(Twitter::class, function() {
            return new Twitter('api-key');
        });
    }

In App\Services\Twitter the class contains...

protected $apiKey;

    public function __construct($apiKey)
    {
        $this->apiKey = $apiKey;
    }

Currently my expected output is for "hello" to be output to the page (I just wanted to check if the 'register' function was being hit at all).

The error I get is...

Illuminate \ Contracts \ Container \ BindingResolutionException Unresolvable dependency resolving [Parameter #0 [ $apiKey ]] in class App\Services\Twitter

edoc's avatar

have you added the class to the providers array

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\BladeServiceProvider::class,
        App\Providers\PaginationServiceProvider::class,
        App\Providers\SocialServiceProvider::class,
    ],
Moggz's avatar
Level 3

Out of curiosity I die-and-dumped a string in the boot method of SocialServiceProvider but that doesn't get hit either.

edoc's avatar

really? it works perfectly on my end though

Moggz's avatar
Level 3

Yes, it looks like this...

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\SocialServiceProvider::class,
    ],
Moggz's avatar
Level 3

I'm using Docker, I don't know if that makes any difference but everything has worked fine until this episode.

I just exec commands into the app php container with...

docker-compose exec app php artisan [command].

Which seems to create the new provider fine.

Moggz's avatar
Level 3

Code from SocialServiceProvider.php:

<?php

namespace App\Providers;

use App\Services\Twitter;
use Illuminate\Support\ServiceProvider;

class SocialServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        dd("hello");
        $this->app->singleton(Twitter::class, function() {
            return new Twitter('api-key');
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        dd("hello 3");
    }
}
Moggz's avatar
Level 3

And the route in web.php:

use Illuminate\Support\Facades\Route;
use App\Services\Twitter;

//Route::get('/', 'PagesController@home');

Route::get('/', function(Twitter $twitter) {
    dd($twitter);
});
Moggz's avatar
Level 3

And the result is...

Illuminate \ Contracts \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $apiKey ]] in class App\Services\Twitter
Moggz's avatar
Level 3

Anyone have any ideas on what could cause this or a resolution?

Moggz's avatar
Level 3

So, to simplify things, I've removed all reference to the Twitter class from the app, and just put dd('hello'); in the SocialServiceProvider. I now get no errors, but "hello" is not printed to the screen, the app just runs as normal and the register method is not hit.

If I put dd('hello'); in the AppServiceProvider it prints 'hello' to the screen and the app does not run. So for now I guess I'll just have to accept that it's not possible to create new service providers.

It seems there must be more to registering a service provider other than just referencing it in config/app.php

Moggz's avatar
Moggz
OP
Best Answer
Level 3

I found a resolution to this.

If anyone else has the same problem where their new ServiceProviders will not register, then add the following code to the register method of the AppServiceProvider:

$this->app->register(
    MyProvider::class
);   

Where 'MyProvider' is the name of your new service provider.

1 like

Please or to participate in this conversation.