, ,

To get Books of Sanjib Sinha…

To know more about Sanjib Sinha….

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web

Composer is a dependency management tool in PHP. For any PHP project you need to use your library of codes. Composer easily manages that task on your behalf helping you to declare those codes. You can also install or update any code in your library through composer. Please visit https://getcomposer.org for more details.
In the opening page of https://getcomposer.org, click the ‘getting started’ link.

Composer Home Page

Composer Home Page

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books.
You can buy from either Leanpub or
Amazon or from my publisher

In the above page you find two links – ‘locally’ and ‘globally’. It stands for two options. Suppose you don’t want to run composer globally or centrally in your system. In that case, you have to download and install composer each time for every project. But the global option is always preferable because once composer is installed in your system bin folder you can call it anytime for any project.

If you are already accustomed with any Linux distribution like Ubuntu you know that for any local PHP project we use to go to ‘/var/www/html’ folder. Suppose we are going to build a simple Laravel project and we want to name it ‘MyFirstLaravelProject’. Open up your Ubuntu terminal (control+alt+t) and first go to that folder first.
To reach there you need to type the following command on your terminal:

cd /var/www/html/

Once you’ve reached, you can make a directory here with a simple command:

sudo mkdir MyFirstLaravelProject

It will ask for your ‘root’ user password. Type the password and a folder called ‘MyFirstLaravelProject’ will be created.
Next in this folder we’ll download and install ‘composer’. Considering you are a beginner, for the sake of brevity I want to download and install composer locally on our Laravel project.
Next issue these two commands – one after another. First you type:

sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

It’ll take some time. Next type this:

sudo php composer-setup.php

It’ll organize your composer setup file to go further. Actually your composer is ready to download packages for your coming project. You can test it by creating a ‘composer.json’ file inside your ‘MyFirstLaravelProject’ folder. In that ‘composer.json’ file type this:

    "require": {
        "monolog/monolog": "1.0.*"

What does this mean? It means you’re installing ‘monolog’ PHP package for your Laravel project. Will it come to any immediate use? The answer is ‘NO’. We’re actually testing our composer installer and want to see how it works.
Now you can issue the command that will install ‘monolog’ package for you. Type this command on your terminal:

php composer.phar install

It’ll take a little time to install the ‘monolog’ package. It depends on your internet speed.
After the installation is over you’ll find a ‘vendor’ folder and few ‘composer’ files inside your project. Feel free to discover what is inside the ‘vendor’ folder. There you’ll find two folders – ‘composer’ and ‘monolog’. Again you can see what they have inside them. As a beginner it’s an endless journey to discover new things. Try to get acquainted with everything new you have found.
The time has come to install Laravel 5.2 through composer. You can install Laravel just like monolog. It means, you can write that instruction in your ‘composer.json’ file and just update your composer. But as a beginner I recommend to follow the simple-most method.
Open up your terminal and write:

sudo composer create-project --prefer-dist laravel/laravel blog

It’ll install Laravel latest version in the folder ‘blog’ in your Laravel project ‘MyFirstLaravelProject’. Once it’s done you’ll get a message on your terminal:

Application key [base64:FrbQTSPezY8wQq+2+bZ/ieA8InA4KjA9N4A44AMbqas=] set successfully.

It’s a random key generated each time you install Laravel. It means you have successfully installed Laravel 5.2.
First step completed: you’ve installed Laravel in ‘/var/www/html/MyFirstLaravelProject/blog’ folder. Now you can go inside that folder and issue a linux command ‘ls -la’ to see what are inside. You can also type ‘php artisan serve’ command to run your first Laravel application so that you can go to http://localhost:8000 to see the welcome page. This installation has been done locally.
There is another easier method. You can install composer globally in your linux system. Open your terminal and make a directory named ‘Code’ on the desktop. Open up your terminal and type:

cd /Desktop

Now you are inside your Desktop. Now you’re going to make the ‘Code’ directory there. Type:

mkdir Code

Now you must go inside your ‘Code’ directory by writing ‘cd Code’ on your terminal. Next inside ‘Code’ folder make a directory ‘test’ where you’ll create your first Laravel application.
Not only that, after the ‘Laravel/Homestead’ is installed, you can type http://test.app to run your first Laravel application. I’ll show you that step by step.
Now it’s time to install ‘composer’ globally. Type these commands one after another.

sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
       sudo php composer-setup.php
  php composer.phar install 

It’ll take few minutes to install composer globally. Once you have done it, you can create any Laravel project anywhere.
Next you can create your first Laravel project inside the ‘Code/test’ folder by typing this command:

sudo composer create-project --prefer-dist laravel/laravel blog

Inside the ‘test’ folder, the Laravel project is installed as ‘blog’. While installing, you can change this name to your choice.
It’ll take a few minutes depending on the speed of your internet connection.
Once done, it’ll give this message:

Application key [base64:FrbQTSPezY8wQq+2+bZ/ieA8InA4KjA9N4A44AMbqas=] set successfully.

You have installed Laravel 5.2.4 latest version on your ‘/Desktop/Cd/test/blog’ folder. And you have done it globally. Feel free to create any folder and sub folders any where in your machine and install Laravel through composer.
Second step completed: Laravel installed in ‘/Desktop/Code/test/blog’ folder and you’ve done it globally.
In the next chapter we’ll learn a little about installing Laravel Homestead.

Want to Communicate?