In this article, I will show you how to create a form package in Laravel and how to use this package. You can check the source code from GitHub directory link: github.com/digitalcrm/Form-Contact-Package.
1. Create Form Package
In this article, I have created a contact form package which will send an email and save the contact query in database. So just follow the steps:
Step1: First, install the Fresh Laravel Project:
Run the command: composer create-project –prefer-dist laravel/laravel BuildForm
Create a folder package in the root directory and create a subfolder contact under this package. This package will do a simple thing which is just sending the email and save the contact details and the messages in the database.
We need at least two things for our form package; one is package.json (composer.json) file and second is service provider (ContactServiceProvider.php). So, run the composer init command which will create the composer.json file in your contact form but after running this command, this will ask various question like package name and vendor, description and author etc… So fill this information.
• After completion of command, you can see composer.json file is created under your package (contact) directory:
• Next define your package under main composer.json file and as well as add autoload psr-4 under package/contact/composer.json file which will tell our composer to there is a package inside Laravel Project.
• After adding autoload psr-4 also add Package Discovery which will help to automatically register its service provider. And this package discovery creates a convenient installation experience for your package’s users.
• Then Run the composer dump-autoload command, which will help to recognize the packages.
Next, we need a service provider for our package, so create a src folder and under this folder create ContactServiceProivder.php file and load the routes, views, migrations and configurations package under boot function like this:
After loading packages, we need to create all files separately under package/contact/src directory as your Form requirements, like this:
• Create src/routes/web.php file and add code like this:
• Create Views file: src/views/contact.blade.php file and code like this:
• Create Controller: src/Http/Controllers/ContactController.php and add code like this:
Create Model and Migrations:
Run the command for creating model and migrations: php artisan make:model Contact –m. This command create model and migration in your laravel project so you have to cut model and migration file and paste under your packages and edit and add code like this:
• Open src/Models/Contact.php file and add code like this:
Creating Database and Configuration:
• Create database: open your phpMyadmin and create the database:
• Next, open .env file and configure database with your laravel project
• As well as configure your mail driver with your laravel projects: Browse mailtrap.io and login in mailtrap.io, you will get your credentials like this:
• Then Configure with your .env file, like this:
• After setting up databases, then run the migrate command: php artisan migrate
Create Mailable system for sending mail:
Run php artisan make:mail ContactMailable –markdown=contact.email. This command will create a ContactMailable.php and email.blade.php file in your main Laravel project. You should have to cut these file along with folder and paste it into your package/contact/src directory.
• Open Mail/ContactMailable.php file and add code like this:
• Open views/contact/email.blade.php file and add code like this:
Under your package/contact/src directory create config/contact.php file:
• Open the config/contact.php file and add code like this:
• For publishing the package, run the commands: php artisan vendor:publish.
• After running this command you will get same contact.php file under your main laravel project config folder like this:
2. Publish in Packagist
Here with a few steps, I will discuss how you can submit your package in packagist.org, just follow the steps:
First you need to open packagist.org and create your account using GitHub:
• Sign in
Next, Upload your package in GitHub directory like this:
Afer moving your package in GitHub.com, then submit your package in packagist.org. So, copy the link address from GitHub library and paste it packgist.org like this:
• After submitting the package in packagist.org, you will get your “composer require” command for using this package:
3. How to use or download this Package:
After uploading and creating the package, with few steps, I will show you how to download and use this package:
Download the fresh Laravel project:
Run the composer command: composer create-project –prefer-dist laravel/laravel TESTPACKG
Download the package:
Run composer require command: composer require digitalcrm/contact. This command will download the contact form in your Laravel project.
After Package installation, publish config file. Run the command:
php artisan vendor:publish –provider=”Digitalcrm\Contact\ContactServiceProvider”
This command will publish a file contact.php in your config directory.
You can see the package is installed in your project. Go to your vendor directory and you will see the contact form with package like this:
Setup Package in your Laravel project:
• After installing package, create database and configure database with .env file.
• Browse mailtrap.io and logged in mailtrap.io then configure with it .env file.
• Next, run the php artisan migrate command.
• Finally navigate URL: http://127.0.0.1:8000/contact in your browser. Then you will get output like this: