Composer helps you easily manage PHP packages

Source: Internet
Author: User
Tags oauth cloud hosting codeigniter composer install
In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIMS, Oauth, and AmazonS3 integration. The disadvantage is that when converting the framework (or you do not need to return the framework at all)

In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIMS, Oauth, and Amazon S3 integration. The disadvantage is that when converting the framework (or you do not need to return the framework at all)

In terms of PHP package management, the development of PHP is very slow. As a result, few programmers are found to use tools such as PEAR. On the contrary, most developers choose their preferred frameworks to Process Code, such as DB interaction, ORIM's, Oauth, and Amazon S3 integration.

The disadvantage is that when you change the framework (or do not need to return to the framework at all), it feels like a nightmare. because it involves using new tools, you must learn everything in it, this is not simple. OK,ComposerTo help you solve these problems.

Introduction

Composer solves the problem by positioning itself as "sticking together all projects. This means that the package can be written, developed, and shared in a certain format, and other developers can easily insert it into the application.

This article will explain how to install and use the Composer package. At the end of the article, you can insert the code block into any framework for experience. You can use CodeIgniter, FuelPHP, Laravel, Symfony2, Lithium, Yii, Zend, and so on.

Install

Composer contains two major logical parts: one is used to store packages, and the other is a command line application to help you discover, download, update, and share code.

 
 
  1. $ cd/path/to/my/project
  2. $ curl -s http://getcomposer.org/installer| php

In the project list, a composer. phar file contains all the logical code line tools. You can run the following code to determine whether the installation is successful.

 
 
  1. $ php composer.phar

After this command is executed, all available commands are displayed.

I personally suggest you use this command:

 
 
  1. $ sudo mv composer.phar /usr/bin/composer

Move this file to the bin directory, which allows you to simplify the command.

 
 
  1. $ composer about

If you are running on Windows, you can download this file and install it through the PHP parser, wherever you are.

Parse the composer. json File

If you are a Ruby programmer, you will think this file is very similar to the Gemfile file, or if you are a Node programmer, you will think it is very similar to the package. json file. Similarly, Composer uses the composer. json file to specify settings and encapsulation based on your application requirements.

In most basic forms, the composer file looks like this:

 
 
  1. {
  2. "require": {
  3. "kriswallsmith/assetic": "*"
  4. }
  5. }

A "assetic" package is required to create and specify any version through "kriswallsmith. You can also specify a special version. You can use the following command instead:

 
 
  1. "kriswallsmith/assetic": "1.0.3"

You can even use this method:

 
 
  1. "kriswallsmith/assetic": "1.0.*"

This has some minor changes, but it will not be upgraded to 1.1.0. Programmers need to pay attention to the slight changes on the interface.

Installation requirements

Now, one or more packages under your composer. json file can be run at this time:

 
 
  1. $ php composer.phar install

Or, if you have listened to my suggestions, you only need to run the following command on a Unix machine:

 
 
  1. $ composer install

You will find that the file is being downloaded and placed in the vendors folder under the application root directory. The following configuration can also be used for this logic:

 
 
  1. {
  2. "require": {
  3. "kriswallsmith/assetic": "1.0.*"
  4. },
  5. "config" : {
  6. "vendor-dir" : "packages"
  7. }
  8. }

Automatic Loading

Automatically loaded in PHP is a bit messy. as developers, they have their own operations. For example, the Smarty package uses its own automatic load. Some developers put multiple classes in one file, or the file name is in lower case, which is too casual!

The official PHP community has created PSR-0 standards to regulate these casual practices. By default, Composer supports this standard. Composer built-in PSR-0 automatic loading mechanism, add the following line of code in the project:

 
 
  1. include_once './vendor/autoload.php';

Obviously, if the directory of the autoload. php file changes, you also need to make corresponding changes in the Code.

Below, you can use the following code in the application:

 
 
  1. use Assetic\Asset\AssetCollection;
  2. use Assetic\Asset\FileAsset;
  3. use Assetic\Asset\GlobAsset;
  4. $js = new AssetCollection(array(
  5. new GlobAsset('/path/to/js/*'),
  6. new FileAsset('/path/to/another.js'),
  7. ));
  8. // the code is merged when the asset is dumped
  9. echo $js->dump();

This is an example of using Assetic. Of course, there are also a lot of namespace code here, but this is done to avoid conflicts between packages.

The essence of naming convention for PSR-0 is:

 
 
  1. \ \( \)*

The following example shows the Buzz HTTP package:

 
 
  1. $browser = new Buzz\Browser;
  2. $response = $browser->get('http://www.google.com');
  3. echo $browser->getLastRequest()."\n";
  4. echo $response;

It looks like a beautifying file_get_contents (), but it processes all types of intelligent logic and processes HTTP Response/Request in the background. You will also find that the namespace syntax is not that strong.

Real world

Currently, most PHP storage relies on the main code library. If you use the Facebook SDK, for example, you just push the version from the GitHub or zip file to your code by copying and pasting it, and then put it in your version control system, will change.

The version and your code are only put in the static file. In a sense, you may forget to upgrade it if you are concerned that Facebook has released a new version. The latest file is displayed on the top.

With Composer, you do not need to pay attention to version changes. You only need to run the updates, and all the updates to be updated will be automatically updated. But why is there a lot of code in your repository? Don't you need them there?

The simplest way is to add vendpoints to your "Ignore" list (for example, gitignore) and let your code go completely away. When deploying code, you only need to run composer install or composer update.

If you want to use it more skillfully, You can manually run the entire process. If you have cloud hosting, you can set hooks to run once the code is released.

Summary

In the future, you will see more Composer and various frameworks have begun to provide multi-level integration. FuelPHP will build a Composer package. CodeIgniter provides automatic loading and is widely used in Symfony2.

Using Composer to add related packages to your project is a good way, you do not need to install the PECLI extension or copy and paste a series of files. That method is outdated and a waste of time. (Compilation: Zhang hongyue)

Market@csdn.net.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.