Demo!

30th July 2006

Tomorrow morning, I get to demo our first Zend Framework powered application to the MD of the client's company. I'm dealing with the IT director there, so it's a little nerve-wracking to be presenting to the boss-man!

The demo is going to go well as it's a pretty damn good application.

Tomorrow I'm gonna write an Active Record type class to make dealing with the database easier. Zend_Db_Table just doesn't cut it!

AkCom: The Akrabat Components for the Zend Framework

20th July 2006

I have a real world project to deliver that will be based on the Zend Framework, so I'm actually putting in time to get a system that works for me.

Some of the work I'm doing, I've decided to release under the same license as the Zend Framework and I've decided to use the name AkCom. I was using Akrabat, but decided that it's too generic for me and I might have other plans for it in the future :)

AkCom mainly exists to provide a place for me to put my extensions to the Zend Framework in order to enable me to write applications more easily. No doubt it'll grow over time!

What's in there, then?

Initially, there are three files in AkCom:

DevelopmentExceptionHandler.php
Minor modifications of Harry Fueck's Pretty Blue Screen
Controller/Action.php
An extension of Zend_Controller_Action that creates views automatically for use in the action. Upon destruct, it renders the view. Recent impovements are based on the ideas of Marcus
Controller/RequestRouter.php
A simple router that picks up controller and action from the $_GET and $_POST arrays. It allows for use of Zend_Filter_Input to hold $_GET and $_POST and also allows for controllers to be in different directories based.

Subversion

AkCom is kept in subversion and is available at http://svn.akrabat.com/svn/AkCom/trunk/.

I'll try and write up my usage of the components as time allows.

Dynamic Tabs Subversion Respository

18th July 2006

With the help of Cliff, I've finally got a publically accessible subversion repository online at http://svn.akrabat.com!

The first project with actual code in it is my Dynamic Javascript Tabs. I've updated the trunk to include the change suggested by Samio so that the "activation" code is not part of the js file and is not part of the htm file.

Zend_Config: Iteration and Arrays

2nd July 2006

An question about iteration, arrays and Zend_Config was posted to the Zend Framework's mailing list by Troy Marker. I thought I'd repeat it here as the solution shows how flexible Zend_Config can be.

The post was:


First off I am using an XML file for the data:

< ?xml version="1.0"?>
<config>
    <lists>
        <root>
            <line1>
                <link>/index/show/gallery/cars/</link>
                <text>Cars</text>
            </line1>
        </root>
    </lists>
</config>

I am trying to iterate the file above to create a list in an array to pass to Smarty. I am using the function below to try this:

        public static function GetList1($rlist)
        {
            $retval = array();
            $count 0;
            $lists Zend::registry('lists')->config->lists;
            foreach($lists[$rlist] as $key => $list) {
                $retval[count]['link'] = $list['link'];
                $count++;
            }
            return $retval;
        }

the $rlist parameter would pass the section of the xml file to put into the array.

The responce I am looking for is:

$retval[0]['link'] = /index/show/gallery/cars/
$retval[0]['text'] = Cars

At a guess, Troy is building a menu type system, but that's by-the-by for the problem.

First of all, a recap on Zend_Config's load system; you must always load one of the top level element's from the file. In the case of this particular XML file, you would have to load <lists>. This is accomplished using the php code:

$config = new Zend_Config(Zend_Config_Xml::load('test.xml''lists'));

Having loaded a top level element, it follows that the $config now "points" to elements inside this element. Thus we can access the information at using:

$link $config->root->line1->link

and $link will contain the string "/index/show/gallery/cars/".

All nice and easy :)

Iteration is also supported by Zend_Config, so if we do:

foreach($config->root->line1 as $key=>$data)
{
    echo "$key = $datan";
}

Then the output is:

link = /index/show/gallery/cars/
text = Cars

At this point when you realise that you need an array of arrays as Troy does, you'd be tempted to use a nested foreach loop to iterate over all the elements to create the array:

$allLines = array();
foreach($config->root as $line)
{
  $thisElement = array();
  foreach($line as $key=>$data)
  {
    $thisElement[$key] = $data;
  }
  $allLines[] = $thisElement;
}

A zend::dump($allLines) looks like this:

$allLines array(2) {
  [0] => array(2) {
    ["link"] => string(25) "/index/show/gallery/cars/"
    ["text"] => string(4) "Cars"
  }
  [1] => array(2) {
    ["link"] => string(27) "/index/show/gallery/family/"
    ["text"] => string(6) "Family"
  }
}

This solves Troy's problem completely! However there is another way! (obviously, otherwise, this entry would be pointless!) Zend_Config provides a helper function for exactly this type of problem: asArray().

asArray() does exactly what it says on the tin: it gives you an array from your config key. This lets us lose one of the foreach loops:

$allLines = array();
foreach($config->root as $line)
{
  $allLines[] = $line->asArray();
}

and a dump() of $allLines is exactly the same as above.

As this array is destined for Smarty, it's likely to be processed using Smarty's {foreach} construct. One useful thing about {foreach} is that it doesn't need a zero indexed array, any array will do. Thus we can probably get away with this code:

$allLines $config->root->asArray();

a dump() of $allLines this time gives:

$allLines array(2) {
  ["line1"] => array(2) {
    ["link"] => string(25) "/index/show/gallery/cars/"
    ["text"] => string(4) "Cars"
  }
  ["line2"] => array(2) {
    ["link"] => string(27) "/index/show/gallery/family/"
    ["text"] => string(6) "Family"
  }
}

As you can see, it's not quite the same as before. This time each element's key in $allLines is now the name of the enclosing element of the XML file. As I said, this probably doesn't matter in 99.9% of cases. Going back to Troy's GetList1() function, we can replace it with:


        public static function GetList1($rlist)
        {
            return Zend::registry('lists')->$rlist->asArray();
        }

Which, I think is pretty cool :)

Zend Framework 0.1.4

1st July 2006

Just in case anyone missed it, Zend Framework 0.1.4 is now out! This is an important release as it has Zend_Config in it, along with a host of other stuff :)