Archive for the ‘Zend Framework’ Category

PHP Wishes 2012 – Interfaces and Standard Web Application Project Layout

Since Santa is listening, below are my PHP wishes for 2012:

  1. Standard Interfaces like Java which are used by many projects so that developers and architects focus on implementation
  2. Standardized Web Application Project Layout just like J2EE Web Application Directory Structure (http://java.sun.com/blueprints/code/projectconventions.html) or Linux Standard Base (http://en.wikipedia.org/wiki/Linux_Standard_Base)

I am learning Symfony 2 over the next coupla weeks, and though I am already familiar with Zend Framework 1, I am looking forward to Zend Framework 2 performance enhancements.

The question is that why is it so hard to get consensus for the betterment of all developers and aiding the momentum of the language forward?

Advertisement

Zend Framework Hidden Gems – Zend_Measure

Just had a bit of a problem today dealing with displaying file sizes of uploaded reports in an application we are upgrading using Zend Framework. When the files are uploaded, the file sizes are stored in bytes, but for display we need to show the sized in KB as the typical file size is between 60k and 2,048KB (2MB).

Looking through http://php.net I came across some scripts that we could use, but I was looking for a versatile re-useable solution. Well that is when I came across Zend_Measure (http://www.framework.zend.com/manual/en/zend.measure.html).

How did we use it, well, very simply:

  1. Added Zend_Measure library files into our application library folder (we do not install all components by default, we add them as we need to)
  2. Created a new Zend_Measure_Binary (http://www.framework.zend.com/manual/en/zend.measure.types.html) class instance
  3. Add the filesize in bytes
  4. Change the type from bytes to kilobytes (the conversion types are available as constants)
  5. Call the toString() method to output the formatted type

Now we have a reusable element which can be used …

Interfacing in the PHP World – Dictator Style

Seems that I have a radical approach to the need for PHP interfacing as suggested by Lucas Smith (http://pooteeweet.org/blog/2008/) then a rebuttal by Herman Radtke (http://www.hermanradtke.com/blog/please-do-not-interface-the-php-world/) and finally Lucas Smith again (http://pooteeweet.org/blog/2014)

I think that the approach for PHP should be:

1) Interfaces in the main PHP tree therefore all have to abide or die, that is how Java gets its notoriety. The SPL was a good start, so now let us spread the love to the rest of the core

2) Else start something like Apache Commons for Java which is the Switzerland, and allows drag and drop or reference implementations. However this is also difficult if there are no common interfaces

3) PHP needs that kind of dictatorship that Java has in order to move forward – dictatorships move things forward (whether good or bad).

Why do I say so?

1. What is an interface – it is a contract for behavior, which means that irrespective of the implementation you will expect the same behavior, which enhances encapsulation and abstraction core object oriented design principles. This is critical as PHP needs to grow past its roots and become truly enterprise.

2. PHP has grown successful because it is simple, but with simplicity comes a price which affects how the future looms. While PHP does not need a JCP like community process (3 – 5 years for Standards), it needs to have some form of commitees which advance different parts and aspects of the language which is now larger than the individual core developers. The interfaces mean that there is a reference implementation which is always followed by big vendor implementations – what PHP needs now are stable processes and standards for adding and managing features.

3. So how does this go, you cannot interface the whole language at a Go, however PHP’s object and procedural classes means that we gotta start from somewhere. And please do not go overboard, keep the interfaces simple and clean … I know there are issues like big vendor interests but they will have to sit together and work things out otherwise you will end up with people who will innovate over the language see Java’s Spring Framework (http://www.springsource.org/)

4. Using the HTTP request/response classes I have only used the Zend Framework versions and not the Symfony 2 versions yet (in a few weeks), however I like the object oriented nature of those … but that is just me 🙂

In closing, we have to learn to pick the best parts of other languages in order to improve whatever we use .

Zend_Navigation Render the SubMenu for Active Menu Item using a Partial

One of the biggest problems with Zend_Navigation helpers is using partials to render sub-menus in order to achieve customized rendering especially when additional properties have been defined.

The approach to the solution is as follows:


// find the first active menu item
$subnav = $this->navigation()->menu()->findOneByActive(1);
// The finder methods return null for no matches so nee to check that
if ($subnav != null) {
// render the matched menu using a partial
echo $this->navigation()->menu()->renderPartial($subnav, 'index/customsubmenu.phtml');
}

Zend Framework – Page Caching

Well along the Zend Framework journey, the biggest issue now is performance, since the applications run on shared hosting accounts. We have caching enabled for the navigation menus, translation, and even Doctrine queries, however the pages still load slow.

Well one of the caching front ends (http://framework.zend.com/manual/en/zend.cache.frontends.html) is the Page front end. The configuration is the same as the other front ends sharing all the properties of the Core front end. While this speeds up the page loading significantly, it is difficult to use with dynamic pages such as lists, since new values are not added until the cache expires.

One way out of this issue is to tag the page caches with the controller name and expire all the pages served by a controller when new data is processed by the controller. This required a bit more digging around, but the following steps worked for us:

  1. Setup the Page cache configuration in the application.ini – remember to disable the Front Controller output buffering
  2. Initialize the page cache in the application bootstrap and store it in the registry, using the Zend_Registry singleton
  3. In the base controller (we setup one where common initialization is done):
  • Add a function to clean the cache for tags that match the controller name
  • Initialize the page cache and start it in an init method, but remember to add the tag with the controller name, using a line similar to $pageCache->setOption(‘tags’, array($this->getRequest()->getControllerName()));
  • Call the function to clear the controller cache in any method that modifies the database information

This seems to work for now, and we are looking for ways to improve its performance.

PS: Special thanks go to @weierophinney for pointing me in the right direction as I pulled hair out of my head.

Update: From August 2011, we stopped using the page caching due to issues we had with session and cookie variables not working. Also an upgrade to Zend Framework 1.11 fixed most of the performance problems which had necessitated us to have page caching in the first place.

Basically we wanted to have a cache for each page for each user which we failed to achieve in testing scenarios.

%d bloggers like this: