How to create package in Bagisto

How to create package in Bagisto

Bagisto is an open source e-commerce based on Laravel and combined with the power of vue js which makes it fast, scalable and seo-freindly. Today in our How To’s series we are going to learn on how to create  package in bagisto. Since Bagisto is based on laravel, standard way to create package for laravel and bagisto is almost same. In futture posts we will be sharing articles to create package to features to Bagisto, let’s start with creating a package. This tutorial assumes you already have a bagisto installation on your server or in your local environment. Detailed infomation to install bagisto is available on Bagisto Documentation website.

1- Create a directory named  “Mega” inside the packages folder in root of the bagisto installation. packages directory is the container of all namespaces of packages. In vanilla installation of Bagisto, you will get the directory names Webkul which contains the packages for bagisto. Namespace or vendor name is a group of entities or classes that are separated from classes of other namespaces with same name so they don’t conflict. Every extension or package that we will develop should be kept in same namespace to avoid the conflict between classes with same name. inside Mega directory create a directory named HelloWorld. HelloWorld is the name of the package that we will be building.

2-  Inside the package directory HelloWorld  create a file named package.json, this package.json file will be used to manage the javascript dependencies. We will learn about the javascript dependencies in furture posts. Also create a file named composer.json this file fill be used to manage the php dependencies. We will learn about the contents of this file

3- Create folder  named src in HelloWorld folder. This src folder will hold most of our code for the package.

4- Now we will start with registering our package so that laravel loads it during bootstrapping. For this we need to create a service provider for our package. This service provider will be used later to register our package. Its a standard practice to name your provider in format  like packageNameServiceProvider.php. For our package we will name it has HelloWorldServiceProvider.php

5- Create a directory named  Providers inside src directory and create file named HelloWorldServiceProvider.php .This file will contain two methods named as boot() and register() .We will the usage of these methods later in this article.

6- Now we will register our package. To do this go to app.php file inside config folder in root of bagisto installation. We will add our HelloWorldServiceProvider in this app.php file to providers array. To do this add below code to $providers array like below:

$providers = [

‘Mega\HelloWorld\Providers\HelloWorldServiceProvider::class
]
After adding your service provider , it will look like as shown in below image:

hello world service provider- how to create package in bagisto

7- Now we will add our package to composer.json which is located in root of bagisto installation. We need to follow this step to enable auto-loading of our package in psr-4. We need to below code to composer.json

"psr-4": {
    "Mega\\HelloWorld\\": "packages/Mega/HelloWorld"
}

after adding this, our composer.json will look like below:

create package in bagisto
8- Now our package is registered and we have added our package in composer.json .Now let’s add some routes in our package which will redirect request to controller. To begin with routing we need to load our routes file and then we will add routes to our routes file. Let’s begin with routing.

9-  Open HelloWorldServiceProvider.php and add below code in Router() method

this->loadRoutesFrom(__DIR__ . '/../Routes/routes.php');

This path can be any path. Just make sure you create the routes.php file in the same path as the path we pass in loadRoutesFrom method

to render a page in browser we need a view file in our package. For this we need to load our view files like we loaded route file. We will use loadViewsFrom method. The code will look like below:
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'megaHelloWorld');

Here megaHelloWorld is the namespace of our package view. We will use this name to mention or use our view files, that we will see in the routes file.

Now let’s add code in routes.php file

Route::view('/hello-world', 'megaHelloWorld::index.helloworld');

view() method of route accepts 2 parameters, one is URL Route and another is view path file that will be returned for the URL passed as first parameter. In megaHelloWorld::helloworld, megaHelloWorld is the same code that we passed as the second parameter in the method that we called in service provider i.e. loadViewsFrom(). helloworld is the name of view file and laravel appends blade.php when loading it. So for megaHelloWorld::helloworld we will create a helloworld.blade.php file in view directory that we passed in loadViewsFrom method.
Apart from view(), we can use Route::get(), Route::post() to use controller.

After updating routes file, we will create a file named helloworld.blade.php in path packages/Mega/HelloWorld/src/Resources/views/index/helloworld.blade.php and add below very simple content in this file.

<h1>Hello World</h1>

Now goto composer.json file of our package and add below code in this file:

{
    "name": "mega/helloworld",
    "description": "Bagisto Hello World",
    "authors": [
        {
            "name": "Team CodeInnovers",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "require": {},
    "autoload": {
        "psr-4": {
            "Mega\\HelloWorld\\": "src/"
        }
    },
    "extra": {
        "laravel": {
            "providers": [
                "Mega\\HelloWorld\\Providers\\HelloWorldServiceProvider"
            ]
        }
    }
}

After the above code is added run below code in your bagisto root.
php composer dump-autoload

now open your browser and hit the below URL
http://localhost/bagisto/public/hello-world
and you will see the content of file helloworld.blade.php

This is all for today’s article. We have understood the basic steps about how to create package in bagisto.  Package code for today’s article is available to download on github. You may download the code from https://github.com/codeinnovers/bagisto-helloworld

Please feel free to connect with us in case of you have any query or doubt. Since bagisto community is very supportive you may join facebook group and bagisto forum.

Add a Comment

Your email address will not be published.