Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Zend Framework 2 Cookbook

You're reading from   Zend Framework 2 Cookbook If you are pretty handy with PHP, this book is the perfect way to access and understand the features of Zend Framework 2. You can dip into the recipes as you wish and learn at your own pace.

Arrow left icon
Product type Paperback
Published in Dec 2013
Publisher Packt
ISBN-13 9781849694841
Length 340 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
joseph@callaa.rs joseph@callaa.rs
Author Profile Icon joseph@callaa.rs
joseph@callaa.rs
Josephus Callaars Josephus Callaars
Author Profile Icon Josephus Callaars
Josephus Callaars
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Zend Framework 2 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Zend Framework 2 Basics FREE CHAPTER 2. Translating and Mail Handling 3. Handling and Decorating Forms 4. Using View 5. Configuring and Using Databases 6. Modules, Models, and Services 7. Handling Authentication 8. Optimizing Performance 9. Catching Bugs Setting up the Essentials Index

Composer and its uses within Zend Framework 2


Composer is a dependency manager tool for PHP, which has been live since the spring of 2011, and is incredibly handy when it comes to getting projects set up with ease.

Composer reads its configuration from a file called composer.json, which is a JSON file that is being read by composer.phar (PHP archive).

We can use Composer to initialize the Zend Framework 2 library when we are using the Zend Framework 2 skeleton application. Other functionalities within Zend Framework 2 include installing new modules or libraries, which we can use to extend our application.

The composer.json file

If we open up the composer.json file we can see that the file has a couple of keys defined, which tells the Composer what it needs to load, and what versions we need. By default, the Zend Framework 2 skeleton application's composer.json will look similar to the following:

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": ">2.2.0rc1",
    }
}

As we can see the file is pretty easy to understand, and the keys are pretty self explanatory, but to be sure we will go through them quickly to make sure we understand what is going on.

  • name: This is the name of the package with the vendor name as the prefix, in this case the vendor is zendframework and the skeleton-application is the package.

  • description: This short description tells us what the package does.

  • license: This is the license the software is licensed under, normally this is one of the numerous open source/software licenses such as the BSD, GPL and MIT licenses. However, a closed-source software license is also available under the key 'proprietary'.

  • keywords: This is an array of keywords that is used when searching for this package on the getcomposer.org website.

  • homepage: Well this is pretty clear, is it not?

  • require: Now this is getting interesting, as this will tell Composer exactly what we need to run our package. In this case it is an array with PHP, where we need Version 5.3.3 or higher and Zend Framework 2 version 2.2.0rc1 or higher. Please note however, that in production we should always avoid a dev Version or a package with a greater than symbol, as it could potentially break our application. Always (please remember!) to get the exact version required when putting the application live.

Although it doesn't say it here, Composer will always install Zend Framework 2 to the vendor directory, as the required section in the composer.json says we need zendframework/zendframework to run our application. Composer knows that it needs to be installed to the vendor directory because the zendframework/zendframework package is of the type library, and that type is always being copied by Composer to the vendor directory.

Upgrading packages

Sometimes we just want to update our libraries, for example, when we know that a bug has been solved in Zend Framework 2's library, and we really want to have it. Fortunately, Composer comes with a great self-update and update command that we have for our disposal.

To update our libraries automatically through Composer, we should execute the following commands in the terminal (this cannot be done properly through the web browser):

$ php composer.phar self-update

First we want to make sure that we are using the latest Composer, as using an outdated Composer might give unnecessary errors.

$ php composer.phar update

This will update all our packages that we have put in the require section of the composer.json to update to the latest (compatible) version. We should be wary, however, that when we want a new package installed, but without the updation rest of the packages, we should use the following command:

$ php composer.phar update vendor-name/package-name

Here vendor-name and package-name are the names of the packages we want to install.

Composer works because all the packages are registered on their website getcomposer.org. In the website they keep all the packages together, and whenever we try to update or install, the composer.phar will connect to the website and retrieve the newest packages.

When we create our own modules or libraries, we can also submit that to the composer website. Submitting to composer's website will create a better community and a better understanding of the dependencies needed when we begin developing certain applications.

See also

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image