Create php project with composer
Project template for creating and initializing a new PHP project from with a composer project or a composer package is named Show
Open the New Project window in Choose the composer projectThe Wizard lets user to choose the composer package, shows the packages inforation, and subsequently searches for other available packages. Creating the project filesThe next step allows to specify PHP version and project format. See the New Project for details. Project is initialized from the specified
composer package using the Note, that specified Related links
IntroductionDependencies are essential for modern development. They save you time and energy. Functionalities you may need for your app like sending e-mails or logging can all be easily included as third party libraries. Thanks to the open source movement, there are many high quality packages to choose from. In the early days, including third-party libraries was cumbersome and error prone but luckily, today we have tools like Composer to help us. Composer is an exceptional dependency manager for PHP. It replaces PEAR and rightfully so. PEAR requires that your project is specially prepared to work with it, where Composer gives you all the freedom you need without any special requirements. One major difference between these two tools is that PEAR installs dependencies globally and Composer installs them locally, in your project structure. PEAR is essentially a package manager and Composer is a dependency manager. That’s where the emphasis is. Composer was inspired by projects like NPM and Bundler. The vast selection of compatible packages are hosted on the official Composer repository called Packagist. These packages are open source so you can contribute to them too. Popular frameworks and tools like Laravel, PHPUnit and Monolog can all be found here. You can even use a specific code revision of the package when including it in your project so you are getting great flexibility. Composer packages are versioned, so you can pin down the exact version of the package you need. This makes porting your project to another machine or to a CI service such as [Semaphore] (https://semaphoreci.com) effortless. In this tutorial, we’ll explore some of the most used Composer features and show how to use them. After following through, you should be comfortable with managing your PHP project’s dependencies with Composer. PrerequisitesThe software you need is as follows:
I’ll be using PHP version 5.6.5, but it’s perfectly fine to use any other version you have as long as it’s newer than 5.3.2. If you don’t have PHP installed, you can do it with phpbrew which makes managing multiple PHP versions on the same machine easy. InstallationComposer can be installed in two different ways. Install LocallyLocal installation will download $ curl -sS https://getcomposer.org/installer | php All settings correct for using Composer Downloading... Composer successfully installed to: /workspace/composer.phar Use it: php composer.phar $ php composer.phar --version Composer version 1.0-dev (1d8f05f1dd0e390f253f79ea86cd505178360019) Install Globally (Recommended)Installing Composer globally is a handy way to have access to the tool from anywhere by just executing the $ curl -sS https://getcomposer.org/installer | php All settings correct for using Composer Downloading... Composer successfully installed to: /workspace/composer.phar Use it: php composer.phar $ sudo mv composer.phar /usr/local/bin/composer $ composer --version Composer version 1.0-dev (1d8f05f1dd0e390f253f79ea86cd505178360019) If you installed PHP with We continue this tutorial with the assumption that Composer has been installed globally. Configuring ComposerComposer is configured with a single
file named { "require": { "symfony/yaml": "2.6.4" } } The only requirement other than the fact that the file has to be in a JSON format, is the inclusion of the Defining DependenciesBy convention, package names consist of the package’s vendor and its project name. This is done in an effort to avoid name conflicts. In our example above, the vendor is The version of the package can be defined in multiple ways. This is where Composer gives you huge flexibility.
A more detailed description of these restrictions can be found on [this page] (https://getcomposer.org/doc/01-basic-usage.md#package-versions). Breaking Out of the DefaultsIf you’re not satisfied with the default settings, you can use the { "config": { "vendor-dir": "dependencies" }, "require": { "symfony/yaml": "2.6.4" } } A full list of Custom package sourcesA package is essentially a directory containing information like the version of the package and the source from where to get the contents of the package. Two types of package sources exist:
Packages from Composer by default uses { "repositories": [ { "type": "vcs", "url": "https://github.com/Seldaek/monolog" } ], "require": { "symfony/yaml": "2.6.4", "monolog/monolog": "1.12.0" } } Repositories are a list of versions for the package. We require a version from the repository the same way we do with Refer to this page for an in-depth explanation of the various options for configuring repositories. Installing dependenciesTo install the dependencies you defined in The installation process will fetch the latest packages according to the constraints we defined. The packages are put in the
Caching dependenciesComposer stores its cache in Let’s see what difference does the installation from cache can make. user@host:~/workspace/project-1/$ composer install --prefer-dist --profile
[6.5MB/0.03s] Loading composer repositories with package information
[6.8MB/0.48s] Installing dependencies (including require-dev)
[47.0MB/12.45s] - Installing symfony/yaml (v2.6.4)
[47.1MB/13.70s] Downloading: 0%[47.1MB/13.71s]
...
[47.1MB/13.93s] Downloading: 100%[47.1MB/13.93s]
[46.8MB/14.37s] Writing lock file
[46.8MB/14.37s] Generating autoload files
[46.8MB/14.37s] Memory usage: 46.77MB (peak: 56.15MB), time: 14.37s Here’s what happens when a cached packaged is reused for another installation: user@host:~/workspace/project-2/$ composer install --prefer-dist --profile
[6.1MB/0.03s] Loading composer repositories with package information
[6.4MB/0.41s] Installing dependencies (including require-dev)
[46.5MB/3.65s] - Installing symfony/yaml (v2.6.4)
[46.9MB/3.65s] Loading from cache
[47.0MB/3.68s]
[46.6MB/4.07s] Writing lock file
[46.6MB/4.07s] Generating autoload files
[46.6MB/4.07s] Memory usage: 46.62MB (peak: 55.65MB), time: 4.07s As you can see, installing cached packages is much faster the second time. This makes a big difference in a project with many dependencies. Locking down dependenciesWhen the
installation process is initiated the first time, it creates a Updating dependenciesFrameworks
and libraries are being improved all the time and if we want to get the latest features and bugfixes, our dependencies need to be kept up to date. This can be achieved simply by using the The Updating all the dependencies however isn’t always optimal. Maybe there’s a breaking change in one of the
updates and you want to keep the old version until you handle it later. Composer packages can be updated one-by-one by letting the composer update symfony/yaml The update command has very similar options to During your time with installing and updating Composer packages, you might meet this error message: Warning: The lock file is not up to date with the latest changes in
composer.json, you may be getting outdated dependencies, run update to update
them.
This happens when even a tiniest adjustment is introduced in AutoloadingYou may have noticed that Composer is generating autoload files
after the packages are successfully installed or updated. More specifically, the file in question is Composer currently supports these autoloading mechanisms:
There’s even an option to autoload your own classes which we will demonstrate next. Autoloading a custom classLet’s see a simple example about how autoloading works. In our root directory
namespace Shapes;
class Square {
static function area($side){
$surface_area = $side * $side;
echo "The squares surface area is $surface_area units\n";
}
} The Add the // composer.json { "autoload": { "psr-4": {"Shapes\\": "source/"} }, "repositories": [ { "type": "vcs", "url": "https://github.com/Seldaek/monolog" } ], "require": { "symfony/yaml": "2.6.4" } } After the custom class is included the autoload file has to be regenerated to reflect the changes. You can see the effect of
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Shapes\\' => array($baseDir . '/source'),'
) Now, let’s create a simple command line script which will use the #! /usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; $side = isset($argv[1]) ? $argv[1] : null; if (isset($side) && ctype_digit($side)) { Shapes\Square::area($side); } else { echo "Please provide a valid size!\n"; } After the file is created, make it executable and run it. $ chmod +x ./calculate_area.php $ ./calculate_area.php 40 The square's surface area is 1600 units Our Wrapping it upIn this tutorial we saw how Composer manages dependencies for PHP projects. This versatile tool is now part of many developers’ arsenal. I hope this article managed to bring you up to speed with Composer. For a detailed overview of the capabilities of the command line interface, refer to the Composer documentation. How do I create a composer project?how to composer create-project laravel. Step 1: Install (Composer) ... . Step 2: Create a new Laravel project. ... . Step 3: Configure virtual host and system host. ... . Important: In order to enable custom virtual hosts to work you need to uncomment below in xamppfiles/etc/https.conf.. Can composer install PHP?Installation PHP composer on Windows:
Step 1: Navigate to the official composer website. Step 2: Then click on the Download button. Step 3: Then click on the Composer-Setup.exe & download the file. Step 4: Then click on “Install for all users”.
What is composer used for in PHP?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.
Can we use composer in Core PHP?Composer is used in all modern PHP frameworks (Symfony, Laravel) and is one of the most recommended tools that solves fundamental issues in the majority of web projects. This article will guide you through basic composer usage and advanced usage for a team environment.
|