SlideShare a Scribd company logo
Composer Best
Practices
Abid H. Malik
Composer: Introduction
● Composer is a tool for dependency management in PHP.
● It allows you to declare the libraries your project depends on
and it will manage (install/update) them for you.
Composer: files
❖ composer.json
➢ This file describes the dependencies of your project and may
contain other metadata as well. It typically should go in the
top-most directory of your project
❖ composer.lock
➢ contains all of the packages and their exact versions, locking the
project to those specific versions.
❖ auth.json
➢ ~/.composer/auth.json (Global)
➢ <proj_dir>/auth.json (local)
➢ contains authentication for private repositories
Composer: repositories
❖ A Composer repository is basically a package source: a place
where you can get packages from.
❖ Packagist.org
➢ Is the main Composer repository.
➢ Aims to be the central repository that everybody uses.
❖ Packagist.com
➢ is a commercial package hosting product offering
professional support and web based management of
private and public packages, and granular access
permissions.
❖ Github - (host your private repositories)
❖ Private Repositories - (to be discussed later)
Composer: Frequently used commands
● composer install
● composer update
● composer update vendor/package
● composer require vendor_name/package_name
● composer require vendor_name/package_name:version
Composer: Best Practices
● Do not run composer update on production.
● If you want to run then you have to run composer install.
● You should never use composer updatewithout argument.
● A better approach to do if composer-updateis needed:
○ Checkout on a dev environment and composer update,
○ Ensure the app is thoroughly tested on a dev environment
○ Then install on live/production with composer install
Composer: Important Commands
● composer update --with-dependencies
○ Updates all packages and its dependencies
● composer update vendor/*
○ Updates all packages from vendor
● composer update --lock
○ Updates composer.lock hash without updating any packages
● composer remove vendor/package
○ Removes vendor/package from composer.json and uninstalls it
● composer update --no-dev
○ This causes composer to skip installing packages listed in
“require-dev”. After which the “composer.autoload” file is not
generated
● composer install --dry-run
○ Simulates the install without installing anything
Composer: Important Commands
● composer outdated
○ Shows a list of installed packages that have updates available
● composer dump-autoload --optimize
○ Generates optimized autoload files
● composer self-update
○ Updates the composer.phar file to the latest version
● composer depends vendor-name/package-name
○ Tell you which other packages depend on a certain package.
● composer info
○ Show information about packages.
Composer : Passing Version
● composer require vendor/pkg "1.3.2"
○ Installs 1.3.2
● composer require vendor/pkg ">=1.3.2"
○ Above or equal 1.3.2
● composer require vendor/pkg "<1.3.2"
○ Below 1.3.2
● composer require vendor/pkg "1.3.*"
○ Latest of >=1.3.0 <1.4.0
● composer require vendor/pkg "~1.3.2"
○ Latest of >=1.3.2 <1.4.0
Composer : Passing Version
● composer require vendor/pkg "~1.3"
○ Latest of >=1.3.0 <2.0.0
● composer require vendor/pkg "^1.3.2"
○ Latest of >=1.3.2 <1.4.0
● composer require vendor/pkg "^1.3"
○ Latest of >=1.3.0 <2.0.0
● composer require vendor/pkg "^0.3.2"
○ Latest of >=0.3.2 <0.4.0
● composer require vendor/pkg "2.0.0-3.0.0"
○ All versions above and including 2.0.0 and below and including 3.0.0
Tilde (~) and caret (^) version constraints in
Composer
The tilde sign
● ~4.1.3 means >=4.1.3,<4.2.0,
● ~4.1 means >=4.1.0,<5.0.0 (most used),
● ~0.4 means >=0.4.0,<1.0.0,
● ~4 means >=4.0.0,<5.0.0.
The caret sign is slightly different:
● ^4.1.3 (most used) means >=4.1.3,<5.0.0,
● ^4.1 means >=4.1.0,<5.0.0, same as ~4.1 but:
● ^0.4 means >=0.4.0,<0.5.0, this is different from ~0.4 and is more useful for
defining backwards compatible version ranges.
● ^4 means >=4.0.0,<5.0.0 which is the same as ~4 and 4.*.
Thank you

More Related Content

PDF
Beginning with Composer - Dependency manager in php
PDF
Leveraging Composer in Existing Projects
PPTX
Nh php may 2014 - composer
PDF
Composer - The missing package manager for PHP
PDF
Composer yourself: a reintroduction to composer
PDF
12 Composer #burningkeyboards
PDF
composer_talk_20160209
Beginning with Composer - Dependency manager in php
Leveraging Composer in Existing Projects
Nh php may 2014 - composer
Composer - The missing package manager for PHP
Composer yourself: a reintroduction to composer
12 Composer #burningkeyboards
composer_talk_20160209

Similar to Composer Best Practices (20)

PPTX
Composer namespacing
DOCX
"The Power of Composer"
PPTX
Composer
PDF
Dependency management with Composer
PDF
Composer intro
PDF
Php Dependency Management with Composer ZendCon 2016
PDF
Composer the Right Way - MM16NL
PDF
Composer the right way
PDF
Composer: Dependency Manager for PHP
PDF
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
PDF
Php Dependency Management with Composer ZendCon 2017
PDF
Composer the right way [SweetlakePHP]
PDF
Game Changing Dependency Management
PDF
Composer the Right Way - PHPSRB16
PDF
Composer The Right Way - 010PHP
PDF
Composer & Drupal
PDF
Composer The Right Way #PHPjhb15
PDF
Composer the right way - DPC15
PDF
Composer for Busy Developers - php|tek13
PDF
Composer The Right Way
Composer namespacing
"The Power of Composer"
Composer
Dependency management with Composer
Composer intro
Php Dependency Management with Composer ZendCon 2016
Composer the Right Way - MM16NL
Composer the right way
Composer: Dependency Manager for PHP
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
Php Dependency Management with Composer ZendCon 2017
Composer the right way [SweetlakePHP]
Game Changing Dependency Management
Composer the Right Way - PHPSRB16
Composer The Right Way - 010PHP
Composer & Drupal
Composer The Right Way #PHPjhb15
Composer the right way - DPC15
Composer for Busy Developers - php|tek13
Composer The Right Way
Ad

More from Abid Malik (6)

PDF
Magento 2: Dependency Injection
PDF
Introduction to Xdebug
PDF
Magento Docker Setup.pdf
PDF
IDE and Toolset For Magento Development
PDF
Introduction to Magento PWA
PPTX
Magento Overview
Magento 2: Dependency Injection
Introduction to Xdebug
Magento Docker Setup.pdf
IDE and Toolset For Magento Development
Introduction to Magento PWA
Magento Overview
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Spectroscopy.pptx food analysis technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf

Composer Best Practices

  • 2. Composer: Introduction ● Composer is a tool for dependency management in PHP. ● It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
  • 3. Composer: files ❖ composer.json ➢ This file describes the dependencies of your project and may contain other metadata as well. It typically should go in the top-most directory of your project ❖ composer.lock ➢ contains all of the packages and their exact versions, locking the project to those specific versions. ❖ auth.json ➢ ~/.composer/auth.json (Global) ➢ <proj_dir>/auth.json (local) ➢ contains authentication for private repositories
  • 4. Composer: repositories ❖ A Composer repository is basically a package source: a place where you can get packages from. ❖ Packagist.org ➢ Is the main Composer repository. ➢ Aims to be the central repository that everybody uses. ❖ Packagist.com ➢ is a commercial package hosting product offering professional support and web based management of private and public packages, and granular access permissions. ❖ Github - (host your private repositories) ❖ Private Repositories - (to be discussed later)
  • 5. Composer: Frequently used commands ● composer install ● composer update ● composer update vendor/package ● composer require vendor_name/package_name ● composer require vendor_name/package_name:version
  • 6. Composer: Best Practices ● Do not run composer update on production. ● If you want to run then you have to run composer install. ● You should never use composer updatewithout argument. ● A better approach to do if composer-updateis needed: ○ Checkout on a dev environment and composer update, ○ Ensure the app is thoroughly tested on a dev environment ○ Then install on live/production with composer install
  • 7. Composer: Important Commands ● composer update --with-dependencies ○ Updates all packages and its dependencies ● composer update vendor/* ○ Updates all packages from vendor ● composer update --lock ○ Updates composer.lock hash without updating any packages ● composer remove vendor/package ○ Removes vendor/package from composer.json and uninstalls it ● composer update --no-dev ○ This causes composer to skip installing packages listed in “require-dev”. After which the “composer.autoload” file is not generated ● composer install --dry-run ○ Simulates the install without installing anything
  • 8. Composer: Important Commands ● composer outdated ○ Shows a list of installed packages that have updates available ● composer dump-autoload --optimize ○ Generates optimized autoload files ● composer self-update ○ Updates the composer.phar file to the latest version ● composer depends vendor-name/package-name ○ Tell you which other packages depend on a certain package. ● composer info ○ Show information about packages.
  • 9. Composer : Passing Version ● composer require vendor/pkg "1.3.2" ○ Installs 1.3.2 ● composer require vendor/pkg ">=1.3.2" ○ Above or equal 1.3.2 ● composer require vendor/pkg "<1.3.2" ○ Below 1.3.2 ● composer require vendor/pkg "1.3.*" ○ Latest of >=1.3.0 <1.4.0 ● composer require vendor/pkg "~1.3.2" ○ Latest of >=1.3.2 <1.4.0
  • 10. Composer : Passing Version ● composer require vendor/pkg "~1.3" ○ Latest of >=1.3.0 <2.0.0 ● composer require vendor/pkg "^1.3.2" ○ Latest of >=1.3.2 <1.4.0 ● composer require vendor/pkg "^1.3" ○ Latest of >=1.3.0 <2.0.0 ● composer require vendor/pkg "^0.3.2" ○ Latest of >=0.3.2 <0.4.0 ● composer require vendor/pkg "2.0.0-3.0.0" ○ All versions above and including 2.0.0 and below and including 3.0.0
  • 11. Tilde (~) and caret (^) version constraints in Composer The tilde sign ● ~4.1.3 means >=4.1.3,<4.2.0, ● ~4.1 means >=4.1.0,<5.0.0 (most used), ● ~0.4 means >=0.4.0,<1.0.0, ● ~4 means >=4.0.0,<5.0.0. The caret sign is slightly different: ● ^4.1.3 (most used) means >=4.1.3,<5.0.0, ● ^4.1 means >=4.1.0,<5.0.0, same as ~4.1 but: ● ^0.4 means >=0.4.0,<0.5.0, this is different from ~0.4 and is more useful for defining backwards compatible version ranges. ● ^4 means >=4.0.0,<5.0.0 which is the same as ~4 and 4.*.