Get Started with Zend Framework 3

The latest major version of Zend Framework was released last year featuring increased performance and PHP 7 support. Here is how to get started with Zend Framework 3.

Similar to earlier ZF versions, you can create the skeleton application project using composer by running the following command.

$ composer create-project zendframework/skeleton-application project

Composer will then download the relevant packages. Be aware that ZF3 requires at least PHP 5.6 to run, and the application’s entry point file index.php is located in the public directory of the skeleton application project.

If you have configured the application correctly, when running it locally or on a web server, you should see the following page appear.

Get

Development mode can be enabled by running through the following command.

$ composer development-enable

The status of development can also be checked.

$ composer development-status

And development mode can be disabled using:

$ composer development-disable

Within the application code, similar to ZF2, modules are added under the modules directory and are loaded via the ModuleManager. There is also the familiar sight of the Module.php that Zend Framework first looks for in a module. However in ZF3, this file has been moved into the src directory from the module directory.

When adding a module, ensure that you define a Module.php class.

Zend Framework also recommends using Composer’s autoloading capabilities. Therefore within the composer.json file in the project root, look for the autoload section which should look like the following by default:

// composer.json
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/"
    }
},

The new module should then be added to the list.

// composer.json
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Yourmodule\\" : "module/Yourmodule/src"
    }
},

There is now a modules.config.php in the ZF3 skeleton application which is included in the application.config.php file. modules.config.php includes a list of modules that ZF3 should load.

// modules.config.php
return [
    'Zend\Router',
    'Zend\Validator',
    'Application',
    'Yourmodule',
];

More information on how to add a basic module to the Zend Framework application will be included in future posts.