A free and open-source book on Zend Framework for beginners


2.3. Application Dependencies

A dependence is some third-party code your app uses. For example Zend Framework 3 is the dependence for your website.

In Composer, any library is called a package. All packages installable by Composer are registered on Packagist.org site. With Composer, you can identify the packages that your app requires and have Composer to download and install them automatically.

The dependencies of the skeleton application are declared in APP_DIR/composer.json file (see below):

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for Zend Framework zend-mvc applications",
    "type": "project",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "mvc",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": "^5.6 || ^7.0",
        "zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
        "zendframework/zend-mvc": "^3.0.1",
        "zfcampus/zf-development-mode": "^3.0",
        "zendframework/zend-mvc-form": "^1.0",
        "zendframework/zend-mvc-plugins": "^1.0.1",
        "zendframework/zend-session": "^2.7.1"
    },
    "autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "ApplicationTest\\": "module/Application/test/"
        }
    },
    "extra": [],
    "scripts": {
        "development-disable": "zf-development-mode disable",
        "development-enable": "zf-development-mode enable",
        "development-status": "zf-development-mode status",
        "serve": "php -S 0.0.0.0:8080 -t public/ public/index.php"
    }
}

What is JSON?

JSON (JavaScript Object Notation), is a text-based file format used for human-readable representation of simple structures and nested associative arrays. Although JSON originates from JavaScript, it is used in PHP and in other languages, because it is convenient for storing configuration data.

In that file, we see some basic info on the skeleton application (its name, description, license, keywords and home page). You will typically change this info for your future websites. This information is optional, so you can even safely remove it, if you do not plan to publish your web application on Packagist catalog.

What is interesting for us now is the require key. The require key contains the dependencies declarations for our application. We see that we require PHP engine version 5.6 or later and several Zend Framework components, like zend-mvc, zend-mvc-form, etc.

The information contained in composer.json file is enough to locate the dependencies, download and install them into the vendor subdirectory. If at any time you determine that you need to install another dependency, you can do that by editing composer.json and adding your dependency in it, and then typing the following commands from your command shell:

php composer.phar self-update
php composer.phar install

The commands above will self-update the Composer to the latest available version, and then install your dependencies. By the way, Composer does not install PHP for you, it just ensures PHP has an appropriate version, and if not, it will warn you.

If you look inside the vendor subdirectory, you can see that it contains a lot of files. Zend Framework 3 files can be found inside the APP_DIR/vendor/zendframework/ directory (figure 2.2).

Figure 2.2. Vendor directory Figure 2.2. Vendor directory

In some other frameworks, another (conventional) way of dependency installation is used. You just download the dependency library as an archive, unpack it and put it somewhere inside of your directory structure (typically, to vendor directory). This approach was used in Zend Framework 1. But, in Zend Framework 3, you are recommended to install dependencies with Composer.


Top