Book Image

Zend Framework 2 Cookbook

By : Josephus Callaars, [email protected]
Book Image

Zend Framework 2 Cookbook

By: Josephus Callaars, [email protected]

Overview of this book

Zend Framework 2 is the latest creation of World Wide Web infrastructure company Zend Technologies Ltd. This new PHP framework comes with tons of features and an attractive way of creating applications. Not only is the overall usability of the technology much better, but it also makes your applications more testable, something that is often overlooked. "Zend Framework 2 Cookbook" will show you how applications are set up in Zend Framework 2 and how you can develop successfully in this massive framework. You will master features like Modules, Views, Controllers, and Authentication. The book also discusses the Event Manager, unit testing, and how to optimize your application. The book begins with a discussion about setting up Zend Framework 2, and you will also look at how the framework itself works. By the end of this book, you will be able to create entire secure applications on your own and make sure they are tested and optimized for performance as well. You will learn about sending and receiving e-mails, translation and localization of the application, and how to set up the framework on a Linux web server. You will also learn how to display data from the application to the user by using different display strategies and renderings. The creation of modules will also be discussed. Then, you will move on to look at how to authenticate users and make sure the developer knows how to pick the best method available. Unit testing, debugging, and enhancing the performance will also be covered in this book. "Zend Framework 2 Cookbook" is a perfect book for anyone who wants to start developing with Zend Framework 2.
Table of Contents (17 chapters)
Zend Framework 2 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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