Search code examples
laravellaravel-5service-provider

Confused - AppServiceProvider.php versus app.php


Where do I specify my bindings exactly? It seems I can do it in either of these files.

config/app.php Inside of 'providers' =>

app/Providers/AppServiceProvider.php Inside of register()


Solution

  • If your bindings are not related to App, 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 that this class exists registering as a Provider in your config/app.php providers list, that is:

    app/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',
    ],