Wednesday, September 18, 2013

Zend framework 2.2 - ZfcUser module Configuration

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
1
2
3
4
5
6
7
8
9
10
{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.5.3",
        "zendframework/zendframework": ">2.2.0rc1",
        "zendframework/zend-developer-tools": "dev-master",
        "zendframework/zftool": "dev-master",
        "zf-commons/zfc-user": "dev-master",
        "doctrine/doctrine-orm-module": "0.7.*",
        "zf-commons/zfc-user-doctrine-orm": "dev-master"
}
}
Save the file and run (from command line):



itadmin@itadmin-AcerPower-Series:/var/www/pod$ composer.phar update


itadmin@itadmin-AcerPower-Series:/var/www/pod$ composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)

  - Installing zf-commons/zfc-base (v0.1.2)
    Loading from cache

  - Installing zf-commons/zfc-user (dev-master fbbe5fd)
    Cloning fbbe5fdeabd9229bf7d1328358c2d95538a6081e

  - Installing doctrine/lexer (v1.0)
    Loading from cache

  - Installing doctrine/annotations (v1.1.2)
    Loading from cache

  - Installing doctrine/collections (v1.1)
    Loading from cache

  - Installing doctrine/cache (v1.1)
    Loading from cache

  - Installing doctrine/inflector (v1.0)
    Loading from cache

  - Installing doctrine/common (v2.4.1)
    Loading from cache

  - Installing doctrine/dbal (v2.4.0)
    Loading from cache

  - Installing symfony/console (v2.3.4)
    Loading from cache

  - Installing doctrine/orm (v2.4.0)
    Loading from cache

  - Installing doctrine/doctrine-module (0.7.2)
    Loading from cache

  - Installing doctrine/doctrine-orm-module (0.7.0)
    Loading from cache

  - Installing zf-commons/zfc-user-doctrine-orm (dev-master 08be7b2)
    Cloning 08be7b2a5e1ee31a3907582261c113531d2bf964


symfony/console suggests installing symfony/event-dispatcher ()
doctrine/orm suggests installing symfony/yaml (If you want to use YAML Metadata Mapping Driver)
doctrine/doctrine-module suggests installing doctrine/data-fixtures (Data Fixtures if you want to generate test data or bootstrap data for your deployments)
doctrine/doctrine-orm-module suggests installing doctrine/migrations (doctrine migrations if you want to keep your schema definitions versioned)
Writing lock file
Generating autoload files


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
1
2
3
4
5
6
7
8
9
10
...
'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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ...
'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:
1
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:
1
vendor/bin/doctrine-module orm:validate-schema
This just validates our database schema and will output something along this:
1
2
3
$ 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:
1
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
1
2
3
4
5
6
7
8
9
'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.

No comments:

Post a Comment