KodeInfo | Learning resources for web and mobile development

PHP the right way - PHP Package Management with Composer

November 9th, 2014 10:47:09 by Imran Iqbal Comments(0) - Views(7522)

Hi ,
Lets find an easy way for package management . Do you remember days we use to download ,extract files and paste inside our libraries folder and manually require them , thats all old style . I will show you how to manage your packages using composer which can do autoloading classes , autoloading namespaces , downloading and requiring packages for us etc just with one command . Say you want to download Amazon Web Services sdk you can go to packagist website and search for aws it will show you package name 

"aws/aws-sdk-php": "2.7.*@dev"

Paste it into our composer file and run 

php composer update

and we are done . It will download aws-sdk into vendor folder and autoload for us .

Installing Composer

Composer has two main logical parts: there is a repository that stores packages, and then there's the command-line application, which helps you find, download, update and share code.
You can download and install composer by following this steps  . Once you are done create a new php project and create a new file name it as composer.json . Copy below lines into composer.json

{
	"name": "kodeinfo",
	"description": "Learning PHP / Laravel",
	"keywords": ["kodeinfo", "php", "laravel"]	
	"require": {
			
	},
	"config": {
		"preferred-install": "dist"
	},
	"minimum-stability": "stable"
}

Above file says our project name is kodeinfo and some description , keywords for it , minimum-stability setting is to only get the packages which are stable and can be used in production site .

Installing First Package

We will download our first package and will try to make it work with our php application . We will use faker which generates fake data for testing . Go to packagist and type faker you will get the package name as below

"fzaninotto/faker": "1.5.*@dev"

In your composer.json in require section paste this and run composer update

"require": {
       "fzaninotto/faker": "1.5.*@dev"
}

You can also install package through command line which is recommended way

php composer require fzaninotto/faker

It will ask to enter version which you want to install , at the time of writing this post lastet version was 1.5 . 

Our application need to find the autoloader and run it so packages we downloaded through composer will be available for our application . Create a new file as index.php and require our autoloader

require '/vendor/autoload.php';

Create a new instance of faker class 

$faker = Faker\Factory::create();

Now we will call text method which generates fake text

die(var_dump($faker->text()));

Our complete index.php is below 

<?php

require '/vendor/autoload.php';

$faker = Faker\Factory::create();

die(var_dump($faker->text()));

In this way you can easily manage your packages , if package developer is adding new features or fixing a bug in package you can easily update your local copy by doing composer update . 
You can use Composer to avoid needing to pay attention to the versions, and just run an update, and commit all the changes. In production do not push vendor directory to your version control instead just run composer update on production machine or best solution will be to setup hooks which run composer install as soon as your new code is pushed .

 

Thanks

KodeInfo

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus