17th Jun 2020

Laravel: Custom Package

Web

Written By, Aarti Maheshwari

Introduction

What is Package?

The Laravel package is an easy way to use any functionality in any Laravel project. Package is like a collection of reusable code that we can use in our project or application by simply adding it. For example, if we want to perform social login, in that case, we don’t have to write the whole code for social login, we can use a package that can perform social login. We can directly use the methods and functions which are well improved and well tested.

We will create a new custom package in Laravel 7 with a basic example. In this example, we will create a contact form and users can send messages to the admin, these messages are stored in the database.

Some steps to follow for creating custom package:

Install Fresh laravel

In CMD/Terminal write this command :

composer create-project –prefer-dist laravel/laravel ContactPackage

Package Folder

Let’s create Package Folder in our root directory

  • ContactPackage
    • packages

Every package consists of two parts: vendor_name/package_name, here vendor_name is laravel and package_name is contact, like laravel/contact

  • laravel
    • contact

Create those folders inside the packages folder

  • packages
    • laravel
      • contact

Create composer.json file

Go to terminal, in terminal go to package_folder and write this command

composer init

After executing this command, some questions display on your CMD window related to your custom package, fill the details.you can see all the details in composer.json file.

Using this command composer.json file created in packages folder

  • packages
    • laravel
      • contact
        • composer.json

Add your custom package in laravel main composer.json file manually

Go to CMD and execute this command composer dump-autoload

Folder Structure

Create scr folder inside packages folder

composer init

  • packages
    • laravel
      • contact
        • scr

We can create one ServiceProvider for registering any migration, view file, controller and config file in register() function.

In your terminal create serviceProvider

php artisan make:provider ContactServiceProvider

Move to this Provider from app/providers to packages/laravel/contact/src/ and also change the namespace from App\Providers to Laravel\Contact.

here folder structure

  • packages
    • laravel
      • contact
        • scr
          • ContactServiceProvider

After that, register this provider in config/app.php like this:

Create Migration

We need to create migration using artisan command

 php artisan make:migration create_contacts_table

Move this from database/migrations to scr/migrations/create_contacts_table.php and add fields according to requirement in migration.

Create Model

Next step is to create using artisan command

php artisan make:model ContactModel

Now add necessary information in the model and change the file location from app/Models to scr/Model/ContactModel.php and also namespace Laravel\Contact..

Create Controller

We need to create controller using artisan command

php artisan make:controller ContactController -r

You can add your logic here, just like I want to add messages and email_ids given by any user in the contacts table, before add data in the database we need to create view file.

Create a new Controller folder inside src folder src/Controller and change the file location from app/Http/Controllers to scr/Controller/ContactController.php and also namespace Laravel\Contact\Controller.

Create View and Route File

We need to create blade file, firstly create new Views folder inside src folder src/Views/contact.blade.php

After that we need to create a route.php file inside the src folder src/routes.php. In routes.php file create Route for calling your controller like this:

Update Service Provider

Here, we need to register every file(migrations, models and so on) in a service provider created by you.

use Illuminate\Support\ServiceProvider;

Publish view files

If you want the users to be able edit views according to requirement then publish view files using artisan command and update service provider.

In ContactServiceProvider file add this line in boot() function:

After this, go to CMD and fire the below command:

php artisan vendor:publish –tag=laravel\contact\ContactServiceProvider

Using this command, the view file is automatically created in laravel main resources folder like this:

  • packages
    • views
      • laravel
        • contact
          • contact.blade.php

Following all these steps we can create a custom package easily.Atlast if you want to publish your package then how to publish a custom package.

Publish Custom Package

So, to publish your package, you have to follow some steps :

  • Create your account on gitlab
  • upload your package on gitlab
  • In browser open packagist.org click on the submit button and set github package url.
  • Click the check button and the package is registered.

Through these steps, you can easily Export/Publish your package.

Conclusion

I hope this blog will be helpful to anyone who needs steps to create a laravel custom package.  Stay tuned for more such tutorials. Thank You !!

Written By,

Web developer at Yudiz Solutions Pvt. Ltd