SlideShare a Scribd company logo
How to Build
Laravel
Package
Using
Composer?
https://www.bacancytechnology.com
Introduction
A Laravel package is a set of reusable classes
that improve the functionality of a Laravel
website. To put it another way, a package is
what WordPress plugins are. Laravel
packages are designed to reduce
development time by encapsulating
reusable functionality in a set of stand-
alone classes that can be used in any Laravel
project.


In this tutorial: How to Build Laravel
Package using Composer, we’ll construct a
Laravel package for a contact form. Let’s get
this party started.
Prerequisite to
Build Laravel
Package using
Composer
Laravel v 5.5 or above is required.
Composer installed on your system. You
may get Composer here if you don’t
already have it.
Here are the prerequisites to build Laravel
package using Composer:
Basic Set-Up and
Installation
Because a package is designed to give more
functionality to a Laravel website, the first
step is to construct a Laravel website. We’ll
use Composer to set it up. Check out the
official Laravel documentation for more
installation options.
$ composer create-project —prefer-dist
laravel/laravel packagetestapp
You’ll need to configure your env file and
specify your app key and other relevant
data after that’s done. Use the following
command in your terminal or manually
transfer the contents of the .env.example to
a new file and save it as .env.
$ cp .env.example .env
$ php artisan key:generate
Once done, use the below command.
Your .env should look like this once you’ve
generated your app key and configured
your database details
Our basic Laravel app is now set up. It’s
time to start working on our package.
Develop high-performance Laravel
applications with Bacancy!
Hire Laravel Developer and witness the
building of your dream product with us. We
have dedicated developers with
extraordinary problem skills. Contact us
today!
Project Structure
Manually generating individual files and
folders
Utilizing a package such as this CLI
utility.
We’ll set up the fundamental essentials of
our package. There are essentially two
methods for accomplishing this:


Here, we will manually generate the files
and directories to understand how each
component fits together.


Here is our package’s folder structure.


Create folders with the following structure
from your root directory.


$ packages/YourVendor/YourPackage/
Because it’s not a good practice to update
the code in the vendor folder, we’ll perform
all of our work outside in the
packages/YourVendor/YourPackage/
directory instead of
vendor/YourVendor/YourPackage/.


When we’re finished releasing our package,
it’ll be available for download under the
vendor folder.


YourVendor is the name of the vendor,
which might be your name or the name of
the customer or business for whom you are
generating the package
.
The package name is represented by
YourPackage. It will be contactform in our
instance.
Let’s begin creating the files and folders
that will comprise our package.
YourVendor
└──contactform
└──src
├──Database
│ └──migrations
├──Http
│ └──controllers
├──Models
├──resources
│ └──views
└──routes
Create Composer File
We’ll initialize the Composer now. Use the
below command to generate composer.json
$ composer init
Because it is interactive, it will ask you a
series of questions to fill up your
composer.json file. If you’re unsure, click
enter to use the default response; if you’re
not sure, you may update it later directly
from the composer.json file.
The composer.json file should now look like
this.
{
"name": "YourVendor/Contactform",
"description": "A contact form package
for laravel",
"authors": [{
"name": "Usama Patel",
"email": "john.doe@email.com"
}],
"require": {}
}


We need to inform composer.json to
autoload our files, so add this code to your
composer.json.
"autoload": {
"psr-4": {
"YourVendorcontactform": "src/"
}
}
Our composer.json file should now look
something like this.
{
"name": "YourVendor/Contactform",
"description": "A contact form package
for laravel",
"authors": [{
"name": "Usama Patel",
"email": "john.doe@email.com"
}],
"require": {},
"autoload": {
"psr-4": {
"YourVendorContactform": "src/"
}
}
}
Create an empty git repository to keep
track of modifications after that (we’ll add
the remote repo later). Use the below
command.
$ git init
Create Service
Provider
Let’s start by adding some files to our
package. To begin, we must identify a
service provider for our package. Laravel
utilizes a service provider to figure out
which files will be loaded and accessible by
your package.
Create a ContactFormServiceProvider.php
file in your src/subdirectory
$ src/ContactFormServiceProvider.php
We need to define a few items within our
service provider:
1. The namespace (which we defined in
our composer.json autoload).
2. The extension (the Laravel class which
our service provider extends)
3. The two mandatory methods that every
service provider must have (at least two
methods are required for every Laravel
package service provider: boot() and
register()).
Add the following lines of code to your
service provider class.
Note: You may replace YourVendor with
your own vendor name in the code below.


// ContactFormServiceProvider.php


<?php namespace
YourVendorcontactform; use
IlluminateSupportServiceProvider; class
ContactFormServiceProvider extends
ServiceProvider { public function boot() { }
public function register() { } } ?>
We need to inform Laravel how to load our
package and utilize its functionalities
because we haven’t deployed it and it isn’t
yet within our vendor folder, so inside the
root of your Laravel project in the
composer. Add the following code to your
JSON.
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"YourVendorContactform":
"packages/YourVendor/contactform/src",
"App": "app/"
}
},
"autoload-dev": {
"psr-4": {
"YourVendorContactform":
"packages/YourVendor/contactform/src",
"Tests": "tests/"
}
},


Depending on your Laravel version, Laravel
may add it for you automatically. If it does,
be sure to skip it.
Then, on your terminal, run the following
command at the root of your app.
$ composer dump-autoload
Let’s check to verify if our package is loaded
appropriately now. Let’s create a route and
load it in the startup method of the
ContactFormServiceProvider.php file
// ContactFormServiceProvider.php
$this-
>loadRoutesFrom(__DIR__.'/routes/web.
php');
The current directory where the file is
located is referred to as __DIR__.
The routes folder we’ll construct for our
package, which will exist in our src
folder, not the default Laravel routes, is
referenced in routes/web.php.
Please take notice of the following:
Add the following code to the web.php file
in our package routes folder.
<?php //
YourVendorcontactformsrcroutesweb.
php Route::get('contact', function(){ return
'Hello from the contact form package'; }); ?
>
Then, in our root config/app.php, we need
to add our new service provider to the
providers’ array as shown below.
// config/app.php
'providers' => [
...,
AppProvidersRouteServiceProvider::cla
ss,
// Our new package class
YourVendorContactformContactFormS
erviceProvider::class,
],
Run the App
php artisan serve
Start your Laravel app now by using the
command.
Go to localhost:8000/contact in your
browser and you should see something
like this:
You might want to know
Top Laravel Packages To Install
Github
Repository:
Laravel Package
Example
Now we know our package is loading
properly we need to create our contact
form. You can find the rest of the code on
this Github Repository.
Conclusion
This was a basic tutorial to get started with
how can you build Laravel package using
Composer. With your newfound
understanding, you can create even more
amazing packages.
Thank you
https://www.bacancytechnology.com

More Related Content

Similar to How to Build Laravel Package Using Composer.pdf

Lampstack (1)
Lampstack (1)Lampstack (1)
Lampstack (1)
ShivamKumar773
 
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
Toufiq Mahmud
 
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
ssuser337865
 
Packaging for the Maemo Platform
Packaging for the Maemo PlatformPackaging for the Maemo Platform
Packaging for the Maemo Platform
Jeremiah Foster
 
Laravel 4 package development
Laravel 4 package developmentLaravel 4 package development
Laravel 4 package development
Tihomir Opačić
 
RHive tutorial - Installation
RHive tutorial - InstallationRHive tutorial - Installation
RHive tutorial - Installation
Aiden Seonghak Hong
 
1 app 2 developers 3 servers
1 app 2 developers 3 servers1 app 2 developers 3 servers
1 app 2 developers 3 servers
Mark Myers
 
How to install laravel framework in windows
How to install laravel framework in windowsHow to install laravel framework in windows
How to install laravel framework in windows
Sabina Sadykova
 
Laravel mail example how to send an email using markdown template in laravel 8
Laravel mail example how to send an email using markdown template in laravel 8Laravel mail example how to send an email using markdown template in laravel 8
Laravel mail example how to send an email using markdown template in laravel 8
Katy Slemon
 
An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
Using Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websitesUsing Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websites
Anna Ladoshkina
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
tutorialsruby
 
PHP
PHP PHP
Laravel 5.3 - Web Development Php framework
Laravel 5.3 - Web Development Php frameworkLaravel 5.3 - Web Development Php framework
Laravel 5.3 - Web Development Php framework
Swapnil Tripathi ( Looking for new challenges )
 
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Lorvent56
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-framework
Marcelo da Rocha
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
Aggelos Synadakis
 

Similar to How to Build Laravel Package Using Composer.pdf (20)

Lampstack (1)
Lampstack (1)Lampstack (1)
Lampstack (1)
 
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
 
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
Laravel (8) php_framework_handbook__start_from_zer_18604872_(z-lib.org)
 
Packaging for the Maemo Platform
Packaging for the Maemo PlatformPackaging for the Maemo Platform
Packaging for the Maemo Platform
 
Laravel 4 package development
Laravel 4 package developmentLaravel 4 package development
Laravel 4 package development
 
RHive tutorial - Installation
RHive tutorial - InstallationRHive tutorial - Installation
RHive tutorial - Installation
 
1 app 2 developers 3 servers
1 app 2 developers 3 servers1 app 2 developers 3 servers
1 app 2 developers 3 servers
 
How to install laravel framework in windows
How to install laravel framework in windowsHow to install laravel framework in windows
How to install laravel framework in windows
 
Laravel mail example how to send an email using markdown template in laravel 8
Laravel mail example how to send an email using markdown template in laravel 8Laravel mail example how to send an email using markdown template in laravel 8
Laravel mail example how to send an email using markdown template in laravel 8
 
An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)
 
Using Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websitesUsing Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websites
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
 
php-and-zend-framework-getting-started
php-and-zend-framework-getting-startedphp-and-zend-framework-getting-started
php-and-zend-framework-getting-started
 
PHP
PHP PHP
PHP
 
Laravel 5.3 - Web Development Php framework
Laravel 5.3 - Web Development Php frameworkLaravel 5.3 - Web Development Php framework
Laravel 5.3 - Web Development Php framework
 
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
 
Getting started-with-zend-framework
Getting started-with-zend-frameworkGetting started-with-zend-framework
Getting started-with-zend-framework
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 

More from Katy Slemon

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdfUncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Katy Slemon
 

More from Katy Slemon (20)

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
 
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdfUncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
 

Recently uploaded

Advanced Techniques for Cyber Security Analysis and Anomaly Detection
Advanced Techniques for Cyber Security Analysis and Anomaly DetectionAdvanced Techniques for Cyber Security Analysis and Anomaly Detection
Advanced Techniques for Cyber Security Analysis and Anomaly Detection
Bert Blevins
 
The Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU CampusesThe Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU Campuses
Larry Smarr
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
huseindihon
 
Calgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptxCalgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptx
ishalveerrandhawa1
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
Aurora Consulting
 
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
Erasmo Purificato
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
BookNet Canada
 
20240704 QFM023 Engineering Leadership Reading List June 2024
20240704 QFM023 Engineering Leadership Reading List June 202420240704 QFM023 Engineering Leadership Reading List June 2024
20240704 QFM023 Engineering Leadership Reading List June 2024
Matthew Sinclair
 
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
Toru Tamaki
 
20240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 202420240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 2024
Matthew Sinclair
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
KAMAL CHOUDHARY
 
Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024
BookNet Canada
 
7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf
Enterprise Wired
 
20240702 Présentation Plateforme GenAI.pdf
20240702 Présentation Plateforme GenAI.pdf20240702 Présentation Plateforme GenAI.pdf
20240702 Présentation Plateforme GenAI.pdf
Sally Laouacheria
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
Observability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetryObservability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetry
Eric D. Schabell
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
rajancomputerfbd
 

Recently uploaded (20)

Advanced Techniques for Cyber Security Analysis and Anomaly Detection
Advanced Techniques for Cyber Security Analysis and Anomaly DetectionAdvanced Techniques for Cyber Security Analysis and Anomaly Detection
Advanced Techniques for Cyber Security Analysis and Anomaly Detection
 
The Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU CampusesThe Increasing Use of the National Research Platform by the CSU Campuses
The Increasing Use of the National Research Platform by the CSU Campuses
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
 
Calgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptxCalgary MuleSoft Meetup APM and IDP .pptx
Calgary MuleSoft Meetup APM and IDP .pptx
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
 
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
Paradigm Shifts in User Modeling: A Journey from Historical Foundations to Em...
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
 
20240704 QFM023 Engineering Leadership Reading List June 2024
20240704 QFM023 Engineering Leadership Reading List June 202420240704 QFM023 Engineering Leadership Reading List June 2024
20240704 QFM023 Engineering Leadership Reading List June 2024
 
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
論文紹介:A Systematic Survey of Prompt Engineering on Vision-Language Foundation ...
 
20240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 202420240705 QFM024 Irresponsible AI Reading List June 2024
20240705 QFM024 Irresponsible AI Reading List June 2024
 
Recent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS InfrastructureRecent Advancements in the NIST-JARVIS Infrastructure
Recent Advancements in the NIST-JARVIS Infrastructure
 
Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024Details of description part II: Describing images in practice - Tech Forum 2024
Details of description part II: Describing images in practice - Tech Forum 2024
 
7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf7 Most Powerful Solar Storms in the History of Earth.pdf
7 Most Powerful Solar Storms in the History of Earth.pdf
 
20240702 Présentation Plateforme GenAI.pdf
20240702 Présentation Plateforme GenAI.pdf20240702 Présentation Plateforme GenAI.pdf
20240702 Présentation Plateforme GenAI.pdf
 
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-InTrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
TrustArc Webinar - 2024 Data Privacy Trends: A Mid-Year Check-In
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
Observability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetryObservability For You and Me with OpenTelemetry
Observability For You and Me with OpenTelemetry
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
 

How to Build Laravel Package Using Composer.pdf

  • 3. A Laravel package is a set of reusable classes that improve the functionality of a Laravel website. To put it another way, a package is what WordPress plugins are. Laravel packages are designed to reduce development time by encapsulating reusable functionality in a set of stand- alone classes that can be used in any Laravel project. In this tutorial: How to Build Laravel Package using Composer, we’ll construct a Laravel package for a contact form. Let’s get this party started.
  • 5. Laravel v 5.5 or above is required. Composer installed on your system. You may get Composer here if you don’t already have it. Here are the prerequisites to build Laravel package using Composer:
  • 7. Because a package is designed to give more functionality to a Laravel website, the first step is to construct a Laravel website. We’ll use Composer to set it up. Check out the official Laravel documentation for more installation options. $ composer create-project —prefer-dist laravel/laravel packagetestapp You’ll need to configure your env file and specify your app key and other relevant data after that’s done. Use the following command in your terminal or manually transfer the contents of the .env.example to a new file and save it as .env. $ cp .env.example .env
  • 8. $ php artisan key:generate Once done, use the below command. Your .env should look like this once you’ve generated your app key and configured your database details
  • 9. Our basic Laravel app is now set up. It’s time to start working on our package. Develop high-performance Laravel applications with Bacancy! Hire Laravel Developer and witness the building of your dream product with us. We have dedicated developers with extraordinary problem skills. Contact us today!
  • 11. Manually generating individual files and folders Utilizing a package such as this CLI utility. We’ll set up the fundamental essentials of our package. There are essentially two methods for accomplishing this: Here, we will manually generate the files and directories to understand how each component fits together. Here is our package’s folder structure. Create folders with the following structure from your root directory. $ packages/YourVendor/YourPackage/
  • 12. Because it’s not a good practice to update the code in the vendor folder, we’ll perform all of our work outside in the packages/YourVendor/YourPackage/ directory instead of vendor/YourVendor/YourPackage/. When we’re finished releasing our package, it’ll be available for download under the vendor folder. YourVendor is the name of the vendor, which might be your name or the name of the customer or business for whom you are generating the package . The package name is represented by YourPackage. It will be contactform in our instance. Let’s begin creating the files and folders that will comprise our package.
  • 15. We’ll initialize the Composer now. Use the below command to generate composer.json $ composer init Because it is interactive, it will ask you a series of questions to fill up your composer.json file. If you’re unsure, click enter to use the default response; if you’re not sure, you may update it later directly from the composer.json file. The composer.json file should now look like this.
  • 16. { "name": "YourVendor/Contactform", "description": "A contact form package for laravel", "authors": [{ "name": "Usama Patel", "email": "john.doe@email.com" }], "require": {} } We need to inform composer.json to autoload our files, so add this code to your composer.json. "autoload": { "psr-4": { "YourVendorcontactform": "src/" } }
  • 17. Our composer.json file should now look something like this. { "name": "YourVendor/Contactform", "description": "A contact form package for laravel", "authors": [{ "name": "Usama Patel", "email": "john.doe@email.com" }], "require": {}, "autoload": { "psr-4": { "YourVendorContactform": "src/" } } }
  • 18. Create an empty git repository to keep track of modifications after that (we’ll add the remote repo later). Use the below command. $ git init
  • 20. Let’s start by adding some files to our package. To begin, we must identify a service provider for our package. Laravel utilizes a service provider to figure out which files will be loaded and accessible by your package. Create a ContactFormServiceProvider.php file in your src/subdirectory $ src/ContactFormServiceProvider.php We need to define a few items within our service provider: 1. The namespace (which we defined in our composer.json autoload). 2. The extension (the Laravel class which our service provider extends) 3. The two mandatory methods that every service provider must have (at least two methods are required for every Laravel package service provider: boot() and register()).
  • 21. Add the following lines of code to your service provider class. Note: You may replace YourVendor with your own vendor name in the code below. // ContactFormServiceProvider.php <?php namespace YourVendorcontactform; use IlluminateSupportServiceProvider; class ContactFormServiceProvider extends ServiceProvider { public function boot() { } public function register() { } } ?>
  • 22. We need to inform Laravel how to load our package and utilize its functionalities because we haven’t deployed it and it isn’t yet within our vendor folder, so inside the root of your Laravel project in the composer. Add the following code to your JSON. "autoload": { "classmap": [ "database/seeds", "database/factories" ], "psr-4": { "YourVendorContactform": "packages/YourVendor/contactform/src", "App": "app/" }
  • 23. }, "autoload-dev": { "psr-4": { "YourVendorContactform": "packages/YourVendor/contactform/src", "Tests": "tests/" } }, Depending on your Laravel version, Laravel may add it for you automatically. If it does, be sure to skip it. Then, on your terminal, run the following command at the root of your app. $ composer dump-autoload
  • 24. Let’s check to verify if our package is loaded appropriately now. Let’s create a route and load it in the startup method of the ContactFormServiceProvider.php file // ContactFormServiceProvider.php $this- >loadRoutesFrom(__DIR__.'/routes/web. php'); The current directory where the file is located is referred to as __DIR__. The routes folder we’ll construct for our package, which will exist in our src folder, not the default Laravel routes, is referenced in routes/web.php. Please take notice of the following:
  • 25. Add the following code to the web.php file in our package routes folder. <?php // YourVendorcontactformsrcroutesweb. php Route::get('contact', function(){ return 'Hello from the contact form package'; }); ? > Then, in our root config/app.php, we need to add our new service provider to the providers’ array as shown below.
  • 26. // config/app.php 'providers' => [ ..., AppProvidersRouteServiceProvider::cla ss, // Our new package class YourVendorContactformContactFormS erviceProvider::class, ],
  • 28. php artisan serve Start your Laravel app now by using the command. Go to localhost:8000/contact in your browser and you should see something like this: You might want to know Top Laravel Packages To Install
  • 30. Now we know our package is loading properly we need to create our contact form. You can find the rest of the code on this Github Repository.
  • 31. Conclusion This was a basic tutorial to get started with how can you build Laravel package using Composer. With your newfound understanding, you can create even more amazing packages.