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
- laravel
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
- contact
- laravel
Add your custom package in laravel main composer.json file manually
"autoload": { "psr-4": { "App\\": "app/", "Laravel\\Contact\\":"packages/laravel/contact", },
Go to CMD and execute this command composer dump-autoload
Folder Structure
Create scr folder inside packages folder
composer init
- packages
- laravel
- contact
- scr
- contact
- laravel
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
- scr
- contact
- laravel
After that, register this provider in config/app.php like this:
'providers' => [ /* * Application Service Providers... */ ... App\Providers\RouteServiceProvider::class, Laravel\Contact\ContactServiceProvider::class, ],
Create Migration
We need to create migration using artisan command
php artisan make:migration create_contacts_table
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateContactTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('contacts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('email'); $table->text('message'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('contacts'); } }
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
<?php namespace Laravel\Contact; use Illuminate\Database\Eloquent\Model; /** * Model of the table tasks. */class ContactModel extends Model { protected $table = 'contacts'; protected $fillable = [ 'title','email','message' ]; }
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.
<?php namespace Laravel\Contact; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Laravel\Contact\ContactModel; class ContactController extends Controller { public function index() { return view('laravel.contact.views.contact'); } public function create() { // } public function store(Request $request) { $contact = ContactModel::create(request->all()); return redirect(route('contact.index')); } public function edit($id) { // } public function update($id) { // } public function destroy($id) { // } }
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
//contact.blade.php <!DOCTYPE html> <html> <head> <title>Contact Us</title> </head> <body> <form action="{{route('contact.store')}}" method="post"> @csrf <label>Title:</label> <input type="text" name="title" placeholder="Enter Title"> <label>Email:</label> <input type="email" name="email" placeholder="Enter Email"> <label>Message:</label> <textarea name="message" placeholder="Enter Message"></textarea> </form> </body> </html>
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:
<?php Route::resource('/contact', 'Laravel\Contact\ContactController');
Update Service Provider
Here, we need to register every file(migrations, models and so on) in a service provider created by you.
<?php namespace Laravel\Contact;
use Illuminate\Support\ServiceProvider;
class ContactServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { $this->loadRoutesFrom(__DIR__.'/routes.php'); $this->loadMigrationsFrom(__DIR__.'/migrations'); $this->loadViewsFrom(__DIR__.'/views', 'contact'); } /** * Register the application services. * * @return void */ public function register() { $this->app->make('laravel\contact\ContactController'); } }
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:
public function boot(){ $this->publishes([ __DIR__.'/views' => base_path('resources/views/laravel/contact'), ]); }
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
- contact
- laravel
- views
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 !!