Stratus Magento 2 Guides - Composer
Instead of identifying, installing, and configuring a PHP library independently, libraries can be specified in a JSON setup file.
Magento 2 relies on composer (though you can avoid it) through several repositories.
Composer for Magento 2 is pre-installed on MageMojo servers. To install, run:
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition
How Composer Works
When Composer is called by this command, it first creates a project and pull in an existing package — in this case, Magento 2. The project will be created, for this example, in the magento/project-community-edition
directory.
Composer retrieves the package from a git repository, as a clone, and runs the composer.json
file to retrieve everything else that may be required for the project.
Composer.json
After the initlal pull, Composer uses the composer.json
to get all the packages.
The following is the composer.json
file for Magento 2.1.1.
{
"name": "magento/project-community-edition",
"description": "eCommerce Platform for Growth (Community Edition)",
"type": "project",
"version": "2.1.1",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"magento/product-community-edition": "2.1.1",
"composer/composer": "@alpha"
},
"require-dev": {
"phpunit/phpunit": "4.1.0",
"squizlabs/php_codesniffer": "1.5.3",
"phpmd/phpmd": "@stable",
"pdepend/pdepend": "2.2.2",
"fabpot/php-cs-fixer": "~1.2",
"lusitanian/oauth": "~0.3 <=0.7.0",
"sebastian/phpcpd": "2.0.0"
},
"config": {
"use-include-path": true
},
"autoload": {
"psr-4": {
"Magento\\Framework\\": "lib/internal/Magento/Framework/",
"Magento\\Setup\\": "setup/src/Magento/Setup/",
"Magento\\": "app/code/Magento/"
},
"psr-0": {
"": "app/code/"
},
"files": [
"app/etc/NonComposerComponentRegistration.php"
]
},
"autoload-dev": {
"psr-4": {
"Magento\\Sniffs\\": "dev/tests/static/framework/Magento/Sniffs/",
"Magento\\Tools\\": "dev/tools/Magento/Tools/",
"Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/Magento/Tools/Sanity/",
"Magento\\TestFramework\\Inspection\\": "dev/tests/static/framework/Magento/TestFramework/Inspection/",
"Magento\\TestFramework\\Utility\\": "dev/tests/static/framework/Magento/TestFramework/Utility/"
}
},
"minimum-stability": "alpha",
"prefer-stable": true,
"repositories": [
{
"type": "composer",
"url": "https://repo.magento.com/"
}
],
"extra": {
"magento-force": "override"
}
}
Parts of this file are obvious according to the name/description pairs within.
The require
section is where the magic begins. This section specifies the packages to be added to the project from the repositories listed. require-dev
is also installed but can be excluded in the initial install with the proper flag (no-dev).
The packages for phpunit and other testing tools are in the vendor
directory in Magento 2. The psr-4
and psr-0
terms are different autoloading specifications in PHP.
config
will check for other items within the path to add to the autoloader.
Autoloading is another useful feature of Composer. Based on the autoload
section, Composer will generate a file named autoload.php
in the vendor
directory. This ties into the bootstrap process index.php
under Magento 2 and automatically includes needed code throughout the store so it can work without having to manually add requirements and other configurations throughout the code.
minimum-stability
and prefer-stable
would only change to specifically use a development or other branch of Magento 2. For the stable releases, this will not change and it is used for production circumstances. A developer interested in contributing to Magento 2 would use this.
repositories
are the code sources and was also part of the initial pull when the Magento 2 install began.
You do not have to use Composer, but it is highly recommended. Modules and themes can integrated into composer to package them together for easy deployment. The alternative is manually (or scripting out) bespoke or custom deployments or relying on git to manage sub-packages in Magento 2. With composer, a Magento 2 site can be quickly created anywhere (minus the specific store data) within minutes.
Feedback
Was this page helpful?
Glad to hear it! Have any more feedback? Please share it here.
Sorry to hear that. Have any more feedback? Please share it here.