How to start working with ZfcUser (ZF2)

I will explain how to setup a Zend Framework Application from the skeleton app in github and then how to install and configure the module called ZfcUser .

The purpose of ZfcUser is:
The purpose of ZfcUser is to provide a flexible foundation for the common practice of user authentication / identification and registration. Ultimately, a user will authenticate via some authentication adapter, and upon successful authentication will be identified by a populated user model for the given session. The focus of ZfcUser is flexibility and more importantly, extendibility. ZfcUser should be able to successfully serve as a starting point for authentication in a very large percentage of ZF2 web applications with widely varying names.

So lets start...

Requirements for this howto:
  • PHP 5.3.3+ (with pdo-sqlite or pdo-mysql), 
    • I'm using php 5.4 that has a built in webserver (note: use it only for dev purposes!!!). 
  • Git
  • sqlite

Install Zend Framework 2 Application 

The process is described here, but basically you install composer:

~> curl -s https://getcomposer.org/installer | php --

Then install the application using composer:

~> php composer.phar create-project --repository-url="http://packages.zendframework.com" zendframework/skeleton-application

There are alternate methods in the skeleton application github page.

At this point we will have ZF2 MVC application installed and working, can check this using the php built in webserver (5.4+),

~>php -S localhost:8888 -t public/

and browsing to http://localhost:8888 with your browser:

Install ZfcUser

Edit the composer.json file and add this line to require ZfcUser. Composer will automatically install ZfcBase thats a dependency of ZfcUser.

composer.json should look like this:

    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "zf-commons/zfc-user": "dev-master"

Then update the dependencies:

~> php composer.phar update

Configure Zend Framework to work with ZfcUser

Add those modules (ZfcBase and ZfcUser) to the application configuration so they can be bootstraped and configured, editing /config/application.config.php:

As you can see in your browser the module is active, browse http://localhost:8888/user

If you try to Sign up, clicking in the link you will receive an Application error, and that simply because we didn't configure the database yet.

Let's setup the database with the schema that comes with ZfcUser. You can locate the folder /vendor/zf-commons/zfc-user/data . ZfcUser provides 3 schemas, one for postgres, one for mysql and one for sqlite. We will use sqlite configuration: schema.sqlite.sql .
From the root folder of the application, run:

~> cat vendor/zf-commons/zfc-user/data/schema.sqlite.sql | sqlite3 data/users.db

Now we need to configure the database adapter so the module can recognize it.
Create a file called database.local.php in the /config/autoload folder and add the configuration:

return array(
    'service_manager' => array(
            'factories' => array(
                'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
    'db' => array(
        'driver' => 'pdo',
        'dsn'    => 'sqlite:'.getcwd().'/data/users.db',


And bam! its done, i can signup:

And it even get my avatar from gravatar.com !!!


Check the wiki to override the default behavior:

2 comentarios:

  1. Nethues Technologies also provide best Zend framework at affordable prices , it is an open source, object oriented web application framework for PHP 5.Read more zend development company.