Tuesday, March 19, 2019

What is Composer in PHP/Laravel? How to Install PHP Composer


Most of you already heard a term "Composer in PHP" but only few of you actually know what actually it is , composer advantages, how it works , how to install composer and finally it’s different commands like composer install, composer update. Well don’t worry as this article going to answer all your questions regarding composer. Moreover, you will also see the practical example of using composer in Laravel.

composer-in-php-laravel


Before understanding about composer in PHP we have to first know about what is "Dependency management". Dependency management simply means managing the dependencies on which the project depends.

Suppose you are working with images and you need a library to manipulate the images in a better and easy way like creating, editing images, adding watermarks on it etc. To achieve this task we can use PHP library named "Intervention", We have to download the library and manually adding this in our code.

For small projects adding libraries manually is not a huge task, But if you have large projects then manually adding them is not a right way to do. Moreover, if you are working with PHP framework like Laravel then you know that Laravel uses different libraries for different purposes i.e authentication etc. Because of the lot of dependencies on which Laravel depends, Recommend way of installing laravel is through  "Composer".

Composer is a dependency manager software which manages the PHP project dependency on a fly. The project should have composer.json file where all the required dependencies are listed, Composer install them automatically.

To use composer in PHP projects, You have to download and install composer in your computer first. Here are the steps:

1-First download the composer

2-Now open the executable file to install it
What is Composer in PHP/Laravel? How to Install PHP Composer

3-Make sure to check developer mode as this will install the composer globally and you don’t have to add the composer path in environment variable to use it.
composer-php


4-Now you have to choose the command line PHP executable file.

installing-composer-php


5- If you are using xammp then click browse and navigate to the root path of your xammp installation and open the PHP folder where you will find a executable file named php.exe, Select it and click next

composer-in-php-laravel
composer-in-php-laravel


6- Now click next without checking proxy server as generally it is not needed (unless you are using some sort of VPN)

composer-in-php

7-Now click finish and you are done with the installation of composer.

Using Composer in PHP/Laravel:


Now we will see a little quick demo on using composer in laravel.

1-Installing any package with composer is just a game of one line, enter one command in command prompt hit enter and from here composer will do the rest of the magic.

2-There are two ways to install Laravel using composer

First way:

1- For installing laravel navigate to the folder where you want to install laravel using cmd.

2-We will use the following command to install laravel:  composer create-project laravel/laravel {directory} {version}

4-This command will simply clone the laravel from the GitHub repo directly to your PC.

5- So in my case I ran following command: Composer create-project laravel/laravel  laravel-practicles  "5.7.*"

6-I first navigate to my htdocs folder then I ran the above command.

7-This command installs the laravel in laravel-practicles folder.

Second way:

1-First, download the laravel installer using composer: composer global require laravel/installer

2-This command will install the laravel package globally in our local system

3-Now to create project for laravel again and again we can just ran following command: laravel new project-name

4-The above command will create a fresh laravel  directory in the specified folder

5-This method is fast and i personally like this.

As you know laravel has many dependencies but since we are using composer to install laravel, All the required dependencies will be automatically installed by composer.

This was the just glimpse of how powerful PHP composer is, I recommend you to use it in your projects when installing any external library as it will be life savior for you.

Let me know in comments if you have any questions/issues regarding composer in PHP, Thanks.


No comments:

Post a Comment