How to publish service provider to the application in laravel 11
V

1

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
    {
        //
    }
}
Variola answered 1/4 at 8:45 Comment(10)
First, try importing your ServerProvider, and then make sure you have given the correct path of PaytmWalletServiceProvider.Goldie
Yes, i have imported the PaytmWalletServiceProvider class in providers file.Variola
Do you get any errors regarding binding? like BindingResolutionException?Goldie
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 workBloomington
@Mit Kathrotia, i didn't get any error but how to i know it is registered and published successfully.Variola
@apokryfos, i have already registered the class in file boostrap/providers.php and also mentioned earlier.Variola
I'm not sure I understand you here. You mention things like "published" implying that this is for package development but you also use config/app.php or bootstrap/providers.php which are not meant to be used for service providers that come from 3rd party packages. What is your use case here?Bloomington
My purpose for register and publish provider class is to use third party package in my application.Variola
Anand\LaravelPaytmWallet that is mentioned in your code (I am assuming that is the one you mean) supports Laravel package discovery so the service provider and aliases should be registered automatically by Laravel. You should not need to register it yourself. In fact most packages written for Laravel are written in a way that supports this.Bloomington
So, should i remove the provider PaytmWalletServiceProvider class from provider folder and file?Variola
S
9

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
];

Shroyer answered 26/4 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.