Posted by & filed under Zend Framework 2.

Every day I work on ZF2, WordPress, and Liferay technologies. The most difficult task I have every day is trying to guide those who do not know a given technology down the path they need to travel without walking down the path with them. I want to point down the right path and see them take off running down it. Occasionally this happens. More often than not it is more along the lines of them starting down the path, coming back for a question about design, starting down the path again, coming back for another design choice, and so on. I am going to walk through making a robust ZF2 web application to point all who are not sure down a path with design choices made for them. You may find yourself disagreeing with some of the design choices I make. If you disagree, throw in your own choice and keep working through it. Understanding ideas, concepts, and methods of implementation are worth far more than simply knowing how to install one specific piece of technology. At the end of this series we will have an application with:

  • Logging
  • Output sanitization
  • User registration, login, password reset, and management
  • Input Filtering and Validation
  • Role-based access control
  • Navigation based on permissions
  • User-based preferences
  • A RESTful API
  • Versioned settings

Using

  • Ubuntu 14.04
  • PhpStorm

Time to dive in. Let’s start by grabbing the ZF2 skeleton application from GitHub.

VCS -> Checkout from Version Control -> GitHub
Screenshot from 2015-04-05 17:35:31

 

With the skeleton cloned you now need a place to run the application.

If you do not have PHP installed, run

apt-get install php5
apt-get install php5-xdebug

Setup your PHP interpreter

 

Screenshot from 2015-04-05 18:20:37

 

 

Screenshot from 2015-04-05 18:20:46

 

Create a built-in web server.

 

Screenshot from 2015-04-05 18:10:37

 

Run composer

php composer.phar install

Run the project and view the skeleton application in your web browser

 

Screenshot from 2015-04-05 18:19:34

 

Now that we have a skeleton up and running let’s add logging to the application. Add

"apache/log4php": "2.3.0"

to the composer.json requirements section and run

php composer.phar update

Add a log4php.xml file in the config directory with

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="default" class="LoggerAppenderFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%d{M d Y H:i:s,u} [%t] %p %c %x - %m%n" />
        </layout>
        <param name="file" value="application.log" />
        <param name="append" value="true" />
    </appender>
    <root>
        <level value="debug" />
        <appender_ref ref="default" />
    </root>
</configuration>

and add

Logger::configure(dirname(__FILE__).'/../config/log4php.xml');

to public/index.php after

require 'init_autoloader.php';

Now when you write PHP classes simply add

private $_log;

public function __construct()
{
    $this->_log = \Logger::getLogger(__CLASS__);
}

to the class then you can use logging within your class anywhere you like. The logger has six levels of logging that can be used (Log levels). Simply use

$this->_log->debug('Testing out the logging.');

As your application grows this will become an invaluable tool.

What do you really think?