on Tutorials

Zend Framework 2 tutorial: Installation and zfcUser module

3 comments
Zend Framework 2 logo
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

This is the first post in new Zend Framework 2 tutorial series in which we will create a fully functional Blog web application with user management using ZfcUser module, Articles, Categories and Comments.

We will learn how to install Zend Framework 2 and Zend Framework 2 Skeleton application, how to install and configure Doctrine2, create modules, forms, validation, relations and plenty more.

When we finish this series, you will have a fully functional base application which you can use how and whenever you see fit in your projects.

Prerequisites

This series assume you have some basic knowledge of Object Oriented PHP. It is aiming on beginner to intermediate level and will show a step by step guides to achieve the set goals.

Zend Framework 2 Tutorial Preparation

First thing we will do is prepare our environment, download Zend Framework 2 Skeleton application and configure virtual host.

So, create your project folder somewhere inside your htdocs (or www) folder. I will call it ZF2BlogTutorial. You can call it whatever you want.

Next, download Zend Framework Skeleton Application (click on Download Zip button there) and unpack it in this newly created folder.

Let’s now create a virtual host for our project. I assume that you know how to create one… For Windows and WAMP users, you can follow this tutorial Here is mine in WAMP:

<VirtualHost 127.0.0.6>
        ServerName *.zf2blog.home
	ServerAlias zf2blog.home
        ServerAdmin zbiskup@gmail.com
        DocumentRoot "C:\wamp\www\Zf2BlogTutorial\public"
	SetEnv APPLICATION_ENV "development"
        Options All
        <Directory "C:\wamp\www\Zf2BlogTutorial\public">
                DirectoryIndex index.php
		AllowOverride All
		Order allow,deny
		Allow from all
        </Directory>
</VirtualHost>

Basically, set Virtual Host to point to public folder of your project. If you followed, you will notice that I have set environment variable APPLICATION_ENV and set it to development. We will see what is that used for later.

Don’t forget to restart your Apache server.

Installing Zend Framework 2 Skeleton application

For the next step we need a Composer, so if you don’t have it, please follow steps on Composer page on how to install it in your operating system.

After you have composer installed and working, run this command in your project directory to install Skeleton Application and resolve any dependencies (I am assuming that you have followed the composer install tutorial from their page and have PHP on your PATH):

composer install

You should see something similar to this:

Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing zendframework/zendframework (2.2.4)
    Downloading: 100%

That’s it, you installed Zend Framework 2 Skeleton Application. If everythhing went well, point your browser to Virtual host domain you set earlier and you should see Skeleton application home page.

Now, we will use that APPLICATION_ENV variable which we set in Virtual Host setup to set error reporting when application is in development environment. So, add this code to project’s main index file:

/public/index.php

...
/**
 * Display all errors when APPLICATION_ENV is development.
 */
if ('development' === $_SERVER['APPLICATION_ENV']) {
	error_reporting(E_ALL);
	ini_set("display_errors", 1);
}
...

Installing and configuring ZfcUser module and Doctrine2

Next task is to install ZfcUser module which will deal with User management in our Blog application and Doctrine2 which will be used as ORM (Object Relational Mapper). This is very easy, we just need to add some lines in our composer.json file and run update command. So, open your composer.json file (in project root folder) and make it look like this (I added other dependencies like Doctrine2 and Zend Developer tools, so we have everything we need to work with):

/composer.json

...
"require": {
	"php": ">=5.3.3",
	"zendframework/zendframework": ">=2.2.2",
	"zf-commons/zfc-user": "dev-master",
	"doctrine/doctrine-orm-module": "0.7.*",
	"zendframework/zend-developer-tools": "dev-master",
	"zf-commons/zfc-user-doctrine-orm": "dev-master"
    }
...

Save the file and run (from command line):

composer update

Let me explain what just happened. Composer just installed all those modules into our project’s vendor folder. Well, for them to work with our application, we need to tell the application they exist, so it can use them properly.

Open /config/application.config.php and let us add those modules so we can use them:

/config/application.config.php

...
'modules' => array(
    	'Application',
    	'DoctrineModule',
    	'DoctrineORMModule',
    	'ZfcBase',
    	'ZfcUser',
    	'ZfcUserDoctrineORM',
    ),
...

If you point your browser to zf2blog.home/user/register and try to register, you will see a PDO exception that it can not connect to the database. Now comes the fun part …

First, create a new database for this and call it zf2blogtutorial (or whatever you want). After that, copy /config/autoload/local.php.dist to /config/autoload/local.php and add this to it:
/config/autoload/local.php

// ...
'doctrine' => array(
		'connection' => array(
				'orm_default' => array(
						'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
						'params' => array(
								'host'     => 'localhost',
								'port'     => '3306',
								'user'     => '****',
								'password' => '****',
								'dbname'   => 'zf2blogtutorial',
							)
					)
			)
	),

Replace username, password, dbname and other things according to your setup. If you try to register now, you will get this exception:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'zf2blogtutorial.user' doesn't exist

Well, of course it doesn’t exist, as we didn’t create it. But, close your PHPMyAdmin, as you don’t need it.

Doctrine ORM module for Zend Framework 2 comes with handy tools which will help us with creating our application from top down. What does this mean?

Well, you first create an Entity and then generate a database schema. We did not create an Entity for ZfcUser, as it already has one included in it’s Doctrine module. So, let’s create the schema.

So, in your command line enter this in project root:

vendor/bin/doctrine-module orm:validate-schema

This just validates our database schema and will output something along this:

$ vendor/bin/doctrine-module orm:validate-schema
[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.

To actually generate the schema, run this:

vendor/bin/doctrine-module orm:schema-tool:update --force

That’s it, you now have a user table in the database and a working Zend Framework2/Doctrine2 based user management… Go ahead and try to register now by going to zfblog.home/user/register.

It is working! Woot! Don’t worry about the looks of ZfcUser screens, that is because Skeleton application is using Twitter Bootstrap 3.0 and ZfcUser 2.*. We will fix that in next part.

Enabling ZendDeveloperTools

Let’s finish today’s session by enabling the handy Zend Developer tools. Open your /config/application.config.php and add Zend Developer Tools to the module list:

/config/application.config.php

'modules' => array(
    	'ZendDeveloperTools',
    	'Application',
    	'DoctrineModule',
    	'DoctrineORMModule',
    	'ZfcBase',
    	'ZfcUser',
    	'ZfcUserDoctrineORM',
    ),

Now, copy /vendor/zendframework/zend-developer-tools/config/zenddevelopertools.local.php.dist to /config/autoload/zenddevelpertools.local.php

If everything is ok, you should see the toolbar on bottom of your application if you refresh the browser.

What’s next?

That concludes our first part of the Zend Framework 2 tutorial series. In next part, we will override the ZfcUser view to be Twitter Bootstrap 3.0 friendly, create Article module and start actually coding something.

Part two is available: Zend Framework 2: Extending ZfcUser Module

If you have any questions, do not hesitate to ask in the comments section below… Stay tuned.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon



  • http://adamczykmikolaj.pl Mikolaj Adamczyk

    Thanks for great tutorial. It tempted me to try Zend2.

  • Peter

    Nice introduction to ZF2. I’m looking forward to the next part in the series.

    It would be great to see how to extend the functionality of ZfcUser to use your own options and a custom user model.

  • ppalmeiro

    Thank you.
    Your post helped me a lot.