5

I want to add service provider in laravel 11, but i am not sure how to add it using laravel 11. As previous version of laravel, it is added in config/app.php file, but in laravel 11 it needs to be added in packageServiceProvider file within providers folder. Below is my code, please tell me if i am wrong somewhere..

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PaytmWalletServiceProvider extends ServiceProvider
{

    /**
     * All of the container bindings that should be registered.
     *
     * @var array
     */
    public $bindings = [
        ServerProvider::class => Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class,
    ];

    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        //
    }
}
10
  • First, try importing your ServerProvider, and then make sure you have given the correct path of PaytmWalletServiceProvider. Commented Apr 1, 2024 at 9:20
  • Yes, i have imported the PaytmWalletServiceProvider class in providers file. Commented Apr 1, 2024 at 9:23
  • Do you get any errors regarding binding? like BindingResolutionException? Commented Apr 1, 2024 at 9:24
  • 1
    Service providers could and still can be placed anywhere. They now are registered in file boostrap/providers.php instead of config/app.php though the old style should still work Commented Apr 1, 2024 at 10:08
  • @Mit Kathrotia, i didn't get any error but how to i know it is registered and published successfully. Commented Apr 1, 2024 at 10:33

2 Answers 2

14

In previous version of Laravel, the providers were registered in config/app.php file. But in Laravel 11.x most of the configurations are moved to /bootstrap directory.

In Laravel 11.x and above, bootstrap/providers.php file returns an array which contains all providers that will be registered in your application.

<?php

return [
    App\Providers\AppServiceProvider::class,
    ...AllYourProvders
];

Sign up to request clarification or add additional context in comments.

Comments

1

An alternative is to call addProviderToBootstrapFile() in your service provider's register() method, passing in the concrete class of your service:


...
use App\Services\PaytmWallet;

class PaytmWalletServiceProvider extends ServiceProvider
{
...
    public function register(): void
    {
         ServiceProvider::addProviderToBootstrapFile(PaytmWallet::class);
    }
...
}

This will add an entry for the concrete class in bootstrap/providers.php array.

2 Comments

have you tested perfomance impact from this method?
Not really, I haven't @keso . Not even considered it tbh because it's the recommended Laravel way so I kinda assumed it's the best way... What are your concerns with this approach? Have you ran any benchmarks at all? I'm very curious if you have any data you could share.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.