How to Create Custom Facade in Bagisto

Updated 17 May 2023

In this blog, we will go over the steps to create a custom facade in Bagisto with a detailed explanation of the code.

Step 1: Create a new Laravel Package
The first step in creating a custom facade is to create a new Laravel package.
First You need to install Bagisto Package Generator if you want to create package by using Bagisto Package Generator.
If you have not installed this Package Generator then you need to go to the root folder of Bagisto and run the following command.

After installing of bagisto-package-generator go to the root directory of your Bagisto project and run the following command:

This command will create a new Laravel package with the name CustomFacade in the packages directory of your Bagisto project.

Step 2: Create a Facade Class
Once you have created the Laravel package, the next step is to create a facade class. A facade is a simple interface that hides the complexity of the underlying code from the user. To create a facade class,create a new folder in the src directory of your package and name it Facade. under the Facade folder create a new file and name it CustomFacade.php. Add the following code to this file:

This code extends the Facade class provided by Laravel and sets the getFacadeAccessor method to return the name of our custom facade.

Step 3: Create a Service Provider
The next step is to create a service provider that will register our custom facade with the Laravel application. A service provider extends the Illuminate\Support\ServiceProvider class and contains two methods: register and boot. The base ServiceProvider class is located in the illuminate/support Composer package, which you should add to your own package’s dependencies. To do this, create a new file in the src directory of your package and name it CustomFacadeServiceProvider.php. Add the following code to this file:

This code creates a new service provider that binds our custom facade to the Laravel application. The bind method takes a closure that returns an instance of our CustomFacade class.

Step 4: Register the Service Provider
The final step is to register our service provider with the Laravel application. All service providers are registered in the config/app.php configuration file. This file contains a providers array where you can list the class names of your service providers. By default, a set of Laravel core service providers are listed in this array. These providers bootstrap the core Laravel components, such as the mailer, queue, cache, and others. To do this, open the config/app.php file in your Bagisto project and add the following code to the providers array:

This code registers our CustomFacadeServiceProvider with the Laravel application.

Step 5: Add your package namespace in psr-4 key in composer.json file for auto loading which is located in Bagisto root directory.

Step 6: Add a Method to the Facade
Now that we have created our custom facade, we can add methods to it that will interact with our application. To add a method to the facade, open the CustomFacade.php file and add the following code:

This code adds a sayHello method to our CustomFacade class that simply returns a string.

Step 7: Use the Facade
Now that we have created our custom facade and added a method to it, we can use it in our Bagisto application. To use the facade, simply call the static method on the CustomFacade class, like this:

This code imports our custom facade and calls the sayHello method on it, which returns the string ‘Hello from CustomFacade!’.

Conclusion: In this blog, we have gone over the steps to create a custom facade in Bagisto. We created a new Laravel package, created a facade class.

Note:I’ve explored this while contributing to Laravel based project Bagisto, there are a lot more things to learn and you could also contribute to an enterprise-level open source Bagisto.

Thanks for reading this blog. I hope you’ll get an idea of how to create Custom Facade. Please share your reviews on this, that will support me to write more.

You can visit the hire laravel developers This platform provides a pool of experienced Laravel developers who can help you with your project requirements and ensure the successful implementation of your ideas. For exploring the available extensions for Bagisto, you can check out extensions.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home