
Photo from http://www.flickr.com/photos/daniello/
Summary
Here I discuss the creation of a view helper for modifying HTML tags, and more specifically body tags. The created view helper allows functionality similar to the head*/inlineScript view helpers already in the standard Zend Framework view helpers, but allows the programmatic modification of tag attributes. Definitely check out the demo page and the code on github.
Motivation
The standard Zend Framework view helpers are a great set of tools for streamlining mundane view tasks and allowing for the modification/addition of scripts and header blocks (generally held in the layout) from within the view without applying ugly hacks (i.e. the head*/inlineScript view helpers).
Upon occasion I have found need to make modifications to the <body> tag, for example adding an onload, class, or style attribute etc. I also required to be able to perform this from within other view helpers. Take this following contrived example…
On website X, certain pages include standard dojo forms. These dojo forms are held within view helpers for convenience. Generally it has been decided not to include the dojo CSS classes in the body tag and only add them when necessary. There maybe several view helpers on the page that need to add their own attributes to the body tag. (I said it was contrived)
The code is available in my GIT repository @ github and the demo page.
Continue reading 'Zend Framework Body Tag View Helper'»
Tweet This Post
Plurk This Post
Buzz This Post
Delicious
Digg This Post
Facebook
MySpace
Ping This Post
Reddit
Stumble This Post
PHP, Web Programming, Zend Framework, Zend_Registry, Zend_View_Helper
|
Body, framework, helper, html, PHP, view, view helper, zend, Zend Framework, Zend_Registry, Zend_View_Helper, Zend_View_Helper_Placeholder_Container_Standalone
Dependency injection is a technique that allows for loosely coupled objects within a software application. Generally if an object requires access to the functionality of another it would be instantiated internally leading to tightly coupled systems. By implementing dependency injection we inject the required objects ready for use (sometimes also referred to inversion of control – IOC). Take the following example:
<?php
class DecisionMaker {
public function makeDecision(array $parameters) {
// Need the database adapter
$dp = new DecisionParameters();
$parameterScore = $dp->getScore($parameters);
/* ... Some more decision logic ... */
return ($parameterScore > 50);
}
}
This piece of code is said to be tightly coupled to the DecisionParameters object. Rewriting the above in a loosely coupled fashion we’d have something like….
<?php
class DecisionMaker {
private $_dp;
public function __construct($dp) {
$this->_dp = $dp;
}
public function makeDecision(array $parameters) {
$parameterScore = $this->_dp->getScore($parameters);
/* ... Some more decision logic ... */
return ($parameterScore > 50);
}
}
Whilst gaining the benefits of loosely coupled code we are adding complexity such that each time an object is instantiated we also have to instantiate its dependencies and pass these in too. For example, this:
$choice = new DecisionMaker();
echo $choice->makeDecision(array('effort' => 'low', 'return' => 'high'));
now becomes:
$dp = new DecisionParameters();
$choice = new DecisionMaker($dp);
echo $choice->makeDecision(array('effort' => 'low', 'return' => 'high'));
This situation becomes more painful as the number of dependencies for a class is increased, and what if the dependencies themselves have dependencies? This can quite quickly become an object administration nightmare! Enter dependency injection containers (or frameworks)…
Continue reading 'Quick Start Symfony DI (Dependency Injection) Tutorial'»
Tweet This Post
Plurk This Post
Buzz This Post
Delicious
Digg This Post
Facebook
MySpace
Ping This Post
Reddit
Stumble This Post
Articles, Dependency Injection Container, PHP, Symfony, Zend Framework, Zend_Cache
|
cache, container, dependency injection, di, framework, ini, inversion of control, IOC, library, PHP, symfony, xml, yaml, zend, Zend Framework, Zend_Cache
Articles, PHP, Web Programming, Zend Framework, Zend_Layout, Zend_View
|
application, framework, helper, PHP, view helper, zend, Zend Framework, zend_layout, zend_view
Articles, Computing, PHP, Web Programming, Zend Framework
|
action, controller, framework, helper, layout, module, PHP, zend, Zend Framework
Articles, PHP, Web Programming, Zend Framework
|
custom, framework, front controller, helper, PHP, Router, view, zend, Zend Framework, Zend_View_Helper_Abstract
PHP, Web Programming, Zend Framework
|
add, bootstrap, container, dynamically, Navigation, pages, PHP, sub-page, view, zend, Zend Framework, Zend_Navigation
Articles, PHP, Web Programming, Zend Framework
|
framework, ini, PHP, Router, Routes, zend, Zend Framework, Zend_Navigation, Zend_Router
I’ve been reading Head First Design Patterns
recently and have decided to write some of the patterns as PHP examples for my own benefit. The first one that I’ve decided to code up is the Observer Pattern. The formal definition of the Observer Pattern is:
The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.
As systems become more loosely coupled making sure that when an event happens all systems that require knowledge of these updates are informed. For example, a blog post, after saving a post we may need to update a search engine (e.g. Lucene), update our sitemap, tags, email subscribed users, etc. The observer pattern allows developers to add additional listeners without editing their observable object. By injecting observers (i.e. a search engine update observer, a sitemap generator, etc) into a subject (i.e. blog post editing system) we can allow the it to perform all the necessary updates without any changes.
Continue reading 'PHP Design Patterns – Observer Pattern'»
Tweet This Post
Plurk This Post
Buzz This Post
Delicious
Digg This Post
Facebook
MySpace
Ping This Post
Reddit
Stumble This Post
Design Patterns, General, PHP
|
application, class, design, Design Patterns, object, object orientated, objected orientated, observable, observer, observer pattern, observers, pattern, PHP, php-oo, subject