paste the file
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?
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
I was't really expecting the Twitter class to do anything at this point. I was expecting the line dd('hello'); to be hit when the register function of the Service provider was called.
I've re-watched the tutorial multiple times (https://laracasts.com/series/laravel-from-scratch-2018/episodes/22) and I don't see what i'm missing :/
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,
],
Out of curiosity I die-and-dumped a string in the boot method of SocialServiceProvider but that doesn't get hit either.
really? it works perfectly on my end though
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,
],
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.
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");
}
}
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);
});
And the result is...
Illuminate \ Contracts \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $apiKey ]] in class App\Services\Twitter
Anyone have any ideas on what could cause this or a resolution?
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
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.
Please or to participate in this conversation.