Tutorial PHP and Packages (Composer)

Lipaydi

1337
VIP
Jul 23, 2015
44
64
112
2169
If you have been developing software with PHP for a long time, you may have noticed that it takes a lot of time to re-download, write, and try to use the current version of some libraries / files. If you are projecting using libraries such as Zend, Symfony and Laravel, you are in need.

What is this?
Composer is dependency management tool for PHP. If your project is heard, it will allow you to define libraries and install and update them for you.


How to use?
I assume you have php installed on your computer. I have XAMPP.
If you no have PHP you can download XAMPP this link.
XAMPP is a PHP development environment and free.

Windows
1) Download the Composer

Download

2) Installing Composer via setup
2170

Select your php.exe path.
2171

2172

Click install and finish the installation.
2173

Then open the CLI (command line interface ex. CMD) and write 'composer'. You will see Composer welcome screen.
2174

Well finished the Composer setup. We can actually do some practice.
I created "composer-test" folder to "C:\xampp\htdocs\composer"-test on my disk.

2175

Open the CMD and write this code.
Code:
cd "C:\xampp\htdocs\composer-test"
2176

Packagist
Composer packages is here.

My choice is geerlingguy/ping
2177

2178

I want to use the package, I need to use the necessary commands.
Code:
composer require geerlingguy/ping
yk6J-KkwT2ueKtjKZ57KwQ.png


Okey ! Now this files on my "composer-test" folder
FxipcUwRRaySsOWqH03R7g.png


I created "index.php":
Code:
<?php
    require_once __DIR__ . '/vendor/autoload.php'; // This is default Composer general including file. One require and included all packages :)

    $host = 'www.google.com'; // Host
    $ping = new \JJG\Ping($host); // Called package.
    $latency = $ping->ping(); // Ping

    if ($latency !== false) {
        print 'Latency is ' . $latency . ' ms';
    }
    else {
        print 'Host could not be reached.';
    }

And finish:
sF3syPRqQtS2I9TslRofKQ.png


Composer Cheat Sheet

Thanks.
 
Last edited:
Top