Confused - AppServiceProvider.php vs. app.php

Where can I specify my bindings exactly? I seem to be able to do this in any of these files.

config / app.php Inside'providers' =>

app / Providers / AppServiceProvider.php Insideregister()

+3


source to share


2 answers


If your bindings are not App related , then I would create a new ServiceProvider class where I overwrite the register method with my new binding, then you have to let Laravel know this class exists by registering as a provider in the config / app.php provider list, then there is:

Application / Providers / MyNewClassServiceProvider.php

<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyNewClassServiceProvider extends ServiceProvider {
 public function register()
 {
    $this->app->bind(
        'App\Repository\MyNewClassInterface',
        'App\Repository\MyNewClassRepository'
    );
 }
}

      



config / app.php

'providers' => [
// Other Service Providers

'App\Providers\MyNewClassServiceProvider',
],

      

+1


source


An array of service providers is loaded via config/app.php

. This is the only place where providers are registered and where you should place service providers.

AppServiceProvider

refers to Laravel-specific services that you have overridden (or actually specified), such Illuminate\Contracts\Auth\Registrar

as the HTTP / Console cores and whatever you want to override in Laravel. It is the only service provider that registers the container bindings that you specify.



Indeed, you can download whatever you want here, but there are tons of ready-made service providers in the directory app/Providers

for you, so you don't have to go and do it yourself.

+2


source







All Articles