Last Updated:

Creating a package for Laravel

In one of the articles "Implementation of a modular structure in Laravel" we implemented a modular system in Laravel. And in this article, let's put all our code in a package for Laravel. Then we will publish the package on github and register on packagist. After that, we can use composer to install our package in any Laravel project.

 

We have implemented functionality that allows us to use the modular structure in Laravel. Remind. In the app folder, there is a Modules folder where the ModulesServiceProvider service provider.php is located, as well as folders with modules. Also in the folder with configuration files is the configuration file module.php, where plug-ins and their settings are stored. OK. Let's transfer all this functionality to the package.

Create a repository Composer

Let's create a local composer repository (local for now). In the root of our project, we will create a packages folder in which we will create a productive-quality folder (this will be the name of the vendor), and already in the productive-quality folder we will create a folder of our modules package.

Now in the console go to the folders/productive-quality/modules. And initialize the composer repository

composer init

package for Laravel 2

Next, we need to enter:

  • The name of the package, in the format <ventor>/<name>
  • Package Description
  • Author, in John Smith john@example.com format
  • Minimum Stability указываем dev
  • Package Type оставляем пустым
  • License []: ставим MIT
  • Then we define the dependencies, we don't have them, so we put no
  • Confirm

Great, we have composer.json

 

Open to edit composer.json. And the first thing we need to do is add a version:

…

"version": "1.0.0",

…

Second, add the startup:

…
"autoload": {
    "psr-4": {
        "productive-quality\\Modules\\": ""
    }
…

Great, you and I have created a local composer repository. Yes, it is still empty. Then we will fill it. Now let's check. Let's install it in our project.

Installing the Local Composer Repository

In order to install a package from a local repository, we need to specify the composer from where to install. To do this, open to edit composer.json, which is located at the root of our project.

Note not in the packages/productive-quality/modules folder, but in the root of the project. In my case it is e:\OpenServer\domains\module.loc

And add:

…
"repositories": [
    {
        "type": "path",
        "url": "packages/productive-quality/modules/"
    }
],
…

Now include the package in the list of dependencies "productive-quality/modules": "*" AND update composer. To do this, go to the root of our project and run the command:

composer update
package for Laravel 3

Perfectly package installed.

And if, we go to vendor, then we have a folder productive-quality, and in it a link to modules (link, because we have this package located locally)

Now let's start filling our package.

Creating a package for Laravel

First, let's transfer the service provider ModulesServiceProvider.php to our packages/productive-quality/modules package

And let's open it for editing.

Change namespace to productive-quality\Modules

Let's add a variable

…
$dir = app_path().'/Modules';
…

 

and replace __DIR__ of $dir.

Let's register the configuration

…
$this->mergeConfigFrom(__DIR__ . '/config/module.php', 'module');
…

we specify from where and where.

And we make our configuration for the module published, add:

…
$this->publishes([
    __DIR__.'/config' => base_path('config'),
]);
…

 

That's basically it. Listing of our service provider:

<?php
    namespace productive-quality\Modules;
    /**
    * Service provider for connecting modules
    */
    class ModulesServiceProvider extends \Illuminate\Support\ServiceProvider
    {
        public function boot()
        {
            // register config
            $this->mergeConfigFrom(__DIR__ . '/config/module.php', 'module');
            $this->publishes([
                __DIR__.'/config' => base_path('config'),
            ]);
            $modules = config("module.modules");
            $dir = app_path().'/Modules';
            // dd($dir);
            if($modules) {
                while (list(,$module) = each($modules)){
                    // dd(file_exists($dir.'/'.$module.'/Routes/routes.php'));
                    // Connect routes for the module
                    if(file_exists($dir.'/'.$module.'/Routes/routes.php')) {
                        $this->loadRoutesFrom($dir.'/'.$module.'/Routes/routes.php');
                    }
                    //Load View
                    //view('test::admin')
                    if(is_dir($dir.'/'.$module.'/Views')) {
                        $this->loadViewsFrom($dir.'/'.$module.'/Views', $module);
                    }
                    //Load the migrations
                    if(is_dir($dir.'/'.$module.'/Migration')) {
                        $this->loadMigrationsFrom($dir.'/'.$module.'/Migration');
                    }
                    //Load translations
                    //trans('test::messages.welcome')
                    if(is_dir($dir.'/'.$module.'/Lang')) {
                        $this->loadTranslationsFrom($dir.'/'.$module.'/Lang', $module);
                    }
                }
            }
        }

         public function register()
         {
         }
    }

All the files you can find on github
Now it remains only to connect this service provider to Laravel. To do this, open the config/app file for editing.php

And in the "Providers" section we add

…
productive-quality\Modules\ModulesServiceProvider::class,
…

All. Now we need to upload our package to github.

Create a repository on github.

Go to github and create a new repository. I will not describe how to create a repository on github (there is nothing complicated there, and there is a lot of information on this issue on the Internet). We have created an empty repository, now our package needs to be fluffed.

Go to the console to the root of our package (in my case packages/productive-quality/modules) and execute the command to create a git repository

git init

Next, we add our files under version control:

git add *

Now we need to check our files:

git commit -m "Modular system in Laravel"

The next step is to connect the remote repository to github

git remote add origin git@github.com:productive-quality/modules.git

Note that you will have a different address

All repazitoria are connected. Now we can flush (upload) our repository to github:

git push -u origin master

package for Laravel 4

Our entire package is on the gita.

 

Super. Now we need to register our project on the packagist.org so that composer knows about our package.

Register the package on packagist

Go packagist.org and log in to our account. Click "Submit" and add a link to the git repository

 

Click "Check". Once again we confirm and all our package is registered on packagist.

 

Great now we can check.

Install the new Laravel package via composer.

Remove the local repository from composer.json of our project, remove the "productive-quality/modules" dependency: "*"

Run the command in the console, being in the root of our project

composer update

The package is removed.

Delete the module.php configuration file from the config folder.

And remove from config/app.php (let's just comment)

…

productive-quality\Modules\ModulesServiceProvider::class,

…

All.

Now let's install our package using composer (no longer from the local repository)

Let's add a dependency to composer.json of the current project

…

"productive-quality/modules": "dev-master"

…

OK. And now through the console, being in the root of our project, we run the command

composer update

package for Laravel 5

Voila, the package was downloaded and installed.

Now we need to publish our configuration file. Artisan will help us here. Execute the command:

php artisan vendor:publish

package for Laravel 6

All configuration file for modules in Laravel is copied.

Let's edit our configuration file (add the Test module) and check. Everything works..

Conclusion.

In this article, we have brought the functionality of the implementation of the modular system in Laravel into a separate package. This package was then published on github and registered on packagist. Now in any Project on Laravel we can use the modular system in Laravel by installing the productive-quality/modules package using composer.