How to Use Packages Composer in PHP Projects

How to Use Packages Composer in PHP Projects

Hello everyone, welcome back at porkaone. On this occasion, we will learn how to use Composer for plugin management in PHP projects. Intrigued?, let's follow the tutorial below.


Composer

Composer is a tool for managing dependencies in the PHP language. Using composer we can easily download the plugins/libraries we need, remove, update and manage them. Because when you work with large projects, applications usually require a lot of libraries. The large number of libraries need to be managed properly, so this is where you need composer.



How to Use Composeer

Our main discussion this time is how to use composer. We will discuss, because there are still many who do not know. Actually, how to use Composer in the real project that you build is very easy. Here's how.

1. Download composer at the following link https://getcomposer.org/Composer-Setup.exe. Then install as usual. Also make sure you have installed PHP on your computer. You can install xampp or lampp to make it easier to install php.

2. Create a new folder named how_to_use_composer in the htdocs folder.

3. Go into the how_to_use_composer folder and run the command below. This command serves to initialize composer.

composer init


4. Then you will be asked to answer a few questions. Just follow the example as shown below.

Menjalankan composer
composer init

After finishing answering all the questions, the composer.json file is automatically created. It contains answers we chose earlier, and there is a list of plugins or libraries that are still empty in the section "require": {}


5. Next we will try to install one of the packages. The package that we install is carbon. Carbon is commonly used to facilitate the processing of time and date. Please run the command below to install carbon.

composer require nesbot/carbon


To see a list of packages that can be downloaded from Composer, you can visit packagist.org. There are lots of packages that you can use to make it easier to build applications and documentation of their use is also available.


6. Ok, after the installation is complete, a vendor folder and composer.lock file will appear. If you open composer.json then the require section has changed.

7. Because we have downloaded the packages. So now it's time for us to use the library. Create a new file named index.php. Then follow the script below.


<?php //must include the script below to use packages require 'vendor/autoload.php'; use Carbon\Carbon; $jakarta_now = Carbon::now('Asia/Jakarta'); $london_now = Carbon::now('Europe/London'); echo "Jakarta: $jakarta_now <br>"; echo "London: $london_now <br><br>"; echo "Is today a holiday?<br>"; if (Carbon::now()->isWeekday()) { echo 'Not a holiday! <br><br>'; } else { echo 'Holiday! <br><br>'; } echo "Length of working: " . Carbon::createFromDate(2020, 1, 1)->age . "years <br>"; ?>


Ok, now it's time for us to do a test run. Please open localhost/how_to_use_composer You can see the results in the image below.

How to Use Composer in PHP
Final


Note:  The way to use each package is different. What is certain is that we usually need to add require 'vendor/autoload.php' to call the vendor's packages.


Ok, that's all for our tutorial this time about how to use composer in a php project. Hopefully useful and can be implemented. If you have questions, please ask in the comments column below. That is all and thank you.

Post a Comment

0 Comments