Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

Tuesday, 4 March 2014

Adventures in Zend Framework 2: Episode 2: The Basics

You've Been Framed!

Zend Framework 2 (ZF2) describes itself as "the most popular framework for modern, high-perfoming PHP applications" [1]. It is an open source project which is primarily used for developing web applications and services which uses "100% object-oriented code and utilises most of the new features of PHP 5.3, namely namespaces, late static binding, lambda functions and closures." [2] It is essentially a library of components which can be used separately or together, designed to work most effectively within an MVC implementation.

My first experiences of ZF2 came through utilising VuFind 2 [3] and much of what I am going to write about ZF2 is done so through the prism of VuFind. With that said, I have begun to use ZF2 in other projects and some of the experience I have of certain components such as Forms and Access Control Lists has come through my own experimentation.

Funny Bones

The first port of call for anyone interested in ZF2 (after reading as much literature as possible) should probably be the Skeleton Application available at http://framework.zend.com/downloads/skeleton-app. It offers a step-by-step guide to getting a ZF2 application started using the most common components like databases and forms and gives a basic introduction to the MVC concept. As part of the installation process, the user will also be encouraged to use Composer [4], a dependency manager for PHP which makes adding and using third party software extremely easy. I also recommend familarising yourself with Git [5] and Github [6] as the preferred version control system.

MVC

The Model-View-Controller software pattern to which ZF2 adheres is basically a means of arranging code into logical but integrated sections, separating a developer's representation of information "from the ways that information is presented to or accepted from the user". [7] The model encapsulates the core code complete with the logic and functions required to generate or manipulate data, the view constitutes any outputted representation of this information whilst the controller effectively acts as a  go-between the model and the view, accepting and converting user input into commands for each.

Explaining how the MVC process works in ZF2 could take up the entire contents of a book, let alone a blog post. At the beginning, I suffered some sleepless nights as my brain tried to make sense of some terribly complex diagrams which I managed to find online. Anything I offer here will necessarily be a gross simplification but it may at least offer a base for further exploration.


1) Bootstrap
All the requirements for running the application (including dependencies) are prepared
2) Route
A user request is matched to a controller and action
3) Dispatch
The controller processes the application logic chain
4) Render
The information is represented to the user
5) Finish
The MVC process is complete
Each of these "phases" have different "hooks" which allow a developer to attach or inject logic when required. Some "hooks" are already predefined (such as just before and just after rendering takes place) but a developer can also add their own. In ZF2, these "hooks" are actually "events" (as it made clear by naming conventions which use "pre", "post", "on" etc as descriptions) and an "Event Listener" is responsible for dealing with the separate MVC phases. I will cover the Event Manager and listening to events in a later post.

This MVC structure is partly revealed in the directory structure of the Skeleton Application:





A simplified flow of  information through a ZF2 application would therefore run something like this:
  1. A user makes a request via a url
  2. Web server redirects force all requests via the index.php file in the public folder (unless the requested filename actually exists e.g. images, css files etc)
  3. index.php begins the auto loading process by requiring init_autoloader.php and initialising any dependencies in the vendor directory (including ZF2 which is a dependency of the Skeleton Application)
  4. index.php begins the MVC process, using the configuration settings found in config/application.config.php. Settings include an array of module namespaces (e.g. "Application"), the locations in which modules should be found (e.g. "modules" and "vendor"), overriding configuration files and options for caching.
  5. Modules are loaded and initialised via the Module Manager, largely through the operation of Module.php (which contains "onBootstrap" and "init" methods plus methods for establishing the module configuration and library location e.g. module/Application/src/Application) and config/module.config.php. Module configuration files are merged with the application configuration to create the final configuration.
  6. The Service Locator (responsible for locating predefined services) plus the Router, View and Event services are set up so that they can be used in the Bootstrap phase.
  7. After bootstrapping, the MVC logic is processed as the route is determined from the url, a controller and action are selected, the model code is executed, the response is determined (and rendered if required) and the request is finished.
Coming Up...

In my next post, I'll highlight some of ZF2's major features. It was only once I'd got my head around them that the ZF2 penny began to drop.

[1] http://framework.zend.com/
[2] http://framework.zend.com/about/
[3] http://www.vufind.org
[4] https://getcomposer.org/
[5] http://git-scm.com/
[6] https://github.com/
[7] http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Monday, 3 March 2014

Adventures in Zend Framework 2: Episode 1: An Ode to VuFind

Beginnings

When I first began working for Swansea University in 2009, I have to admit, I was more of a scripter than a developer. Having studied history at university and arriving in the job with no official qualifications in computing, my suitability for the role was based entirely on personal study and experimentation.

My knowledge of front end web desgin and PHP had largely been acquired  via a general computing interest, a year's stint in a primary school I.C.T. department, several years experience in Swansea Public Library Service and the odd request to build a website for a friend or acquaintance.

Fortunately, the primary piece of software for which I was to be responsible - VuFind [1] - was written in PHP. By examining the source code and using the excellent user community, I was able to demonstrate enough familiarity with the VuFind to propose a development path to suit the goals of the project.

VuFind: OOPs I did it again

The first lesson I received from VuFind (or more accurately from the user community, particularly Demian Katz, the project leader) was Object Orientated Programming (OOP) [2]. Up until that point, I had only ever used PHP's native functions, perhaps augmented with a few libraries which had been recommended in "Teach Yourself" type books which were accessed via includes.

Moving from scripting to OOP was probably the most important step I have taken in the last five years. OOP not only makes developing applications far more efficient as frequently used code can be put into methods to be utilised whenever required, it also opens up a world of tried and tested methodologies which can speed up the development process, resulting in code which is more robust, portable and easier to debug.

For the first time, I wasn't just writing code to get things done - I was writing code mindful of considerations such as accepted standards, performance, portability, repository integration and usefulness to others. It felt good to be able to make some small contributions to the direction of the VuFind project and to see some of my patches integrated into the core repository.

VuFind: The Gift that keeps on giving

VuFind 1.0 had introduced me to a world of inheritance, dependency injection, modular programming, encapsulation, interfaces and abstracts. It had also forced me to learn more about the Apache web server, to take my first steps into Unix via a Virtual Box installation of Ubuntu and to consider repository management via SVN. By the time VuFind was launched for the South West Wales Higher Education Partnership (SWWHEP), I was responsible for building and configuring an Ubuntu server from scratch on a VMWare platform.

My experience on the SWWHEP project was also responsible for landing me a permanent job at Swansea University as part of the Web Team. Again, this was a fantastic opportunity as I would be working with professional developers who were willing to help me fill in the gaps which still existed as a result of my rather ad hoc career. If VuFind primarily helped expand what I was able to develop, the move to the web team greatly improved how I was able to develop code. I began to use an integrated development environment for the first time, switched to Unix via Fedora and was introduced to concepts such as test driven development, automated deployment and continuous integration.

VuFind & Zend

VuFind 1.0 had a loose, custom MVC framework. As the project began to grow however, the user community decided that it would be beneficial to base future developments on a tried and tested framework. VuFind 2.0 was initially developed on Zend Framework 1.8 but as Zend Framework 2 was released during this process, VuFind was eventually re-factored to take advantage of these developments. I had never used a framework before and when the beta versions of VuFind 2.0 were released, I realised that Zend 2 used many concepts outside of my comfort zone. As with OOP however, thanks to Demian Katz and the VuFind community, I was able to ask questions and get very helpful answers. Working with the web team also meant that I could tap into their knowledge to resolve particular issues.

Using the experience gained working with VuFind 2, I plan to write a series of brief articles on using the Zend 2 platform. I don't expect to provide any great revelations or insights as I am definitely a consumer rather than an innovator when it comes to programming. Nevertheless, the articles should at least serve me as "revision notes" and they may save some poor soul from hours agonising over an issue which has a simple solution, something which became apparent to me only after the fact!

[1] www.vufind.org
[2] http://en.wikipedia.org/wiki/Object-oriented_programming