How to create a package in Bagisto v2.0?

Updated 7 December 2023

Here in this blog, we are going to see, how to create a package in bagisto v2.0

There are two ways to create a package.

  1. By using Bagisto Package Generator (Recommended)
  2. By manually setting up all files (Expert Level)

However, we are using the second method to design and develop the package. In this blog post, we are creating the “Blog” package as its directory structure as shown below.

Folder Structure:-

Step:1 Create Composer file.

Inside the “Blog” directory, you need to create composer.json file and write the below code and make changes as per needs. Alternatively, you can execute the command

then it will ask some questions regarding package generation answer them carefully.

Now we need to create a directory named providers and inside this directory need to create BlogServiceProvider.php file.

Step:3 Register the package.

Now we need to register the packages in composer.json file inside autoload>psr-4, as you can see below how to register the package.

Packages Registration

Now navigate the config/app.php file then update the providers key and add the Webkul\Blog\Providers\BlogServiceProvider::class at the end of the array.

Package registration

Now we need to create routes, resources(views), models and controllers as per needs.

Step:4 Routes Registration.

Now we need to create the directory Routes inside the blog and file named web.php, make the routes in our case we are just demoing the how route registered.

Route file demo.

Registration in BlogServiceProvider.php file which is inside the Providers directory, in the boot method, need to register like the below code

Step:5 Resources(views) Registration.

Now we need to create the directory Resources inside the packages and register inside the boot method of BlogServiceProvider.php file with the help of loadViewsFrom method which takes only two arguments first one is path of the views and the second one is the namespace(alias) of the views.

if you want to create the models and controllers then you can generally create directories for it, for models and controllers create directories accordingly.

Now we need to register the component in BlogServiceProvider.php file and inside the boot method structure of the Packages are shown below.

Now register the anonymous component inside the boot method in BlogServiceProvider.php file.

if you want to design your own template then you have to create own page structure and design implementation alternatively you can use the default admin or shop package layouts

  1. For Shop template:-

2. For Admin template:-

Step:6 Configuration Vue and Tailwind Css.

Open the terminal into root directory of “Blog” packages and then execute the

This will ask some questions regarding the package.json file generation answer it carefully, or you can copy the below demo and then execute the below command

Now run the below command.

After the installation of the packages, it will generate the node_modules directory and package-lock.json file, create postcss.config.jstailwind.config.js and vite.config.js file into the root directory of Blog packages.

in postcss.config.js file (For Post css configuration).

in tailwind.config.js file (For Tailwind configuration).

in vite.config.js file (For Vite configuration).

Once you’ve integrated your package into a Laravel project, seamlessly incorporating it into the framework involves creating essential components such as routes, views, models, and controllers, begin by defining routes in the web.php file, ensuring clear navigation and linking to your package’s functionality.

Design views to provide an intuitive and engaging user interface, enhancing the user experience. Models play a crucial role in managing data, so establish eloquent models tailored to your package’s data structure. Controllers act as intermediaries, handling user requests and orchestrating interactions between models and views.

By following Laravel conventions, you can effortlessly utilize your package as an integral part of your Laravel application, harnessing its features with the same ease and familiarity as other Laravel components. This streamlined integration enhances code maintainability and facilitates a cohesive development experience.

Let us know your thoughts in the comments below. If you’re looking to build something amazing with Laravel, why not consider hiring laravel developers who can bring your vision to life?

You can also check out our Bagisto Extensions.

Thanks for reading.

. . .

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