Archive

Archive for November, 2006

Discovering the Zend Framework (0.2.0 Preview)

November 11th, 2006 Louis-Philippe Huberdeau 4 comments

At ZendCon, I heard there was a new version of the Zend Framework being released. The version number is 0.2.0. That does not sound very stable, but I decided to give it a try anyway, something I did not do back when 0.1.0 was released. This was quite an adventure. The new release is incorporating major changes in the MVC framework and quite a few other libraries. Most of the changes in the MVC section are made to improve testability, or so I was told. Since new changes may break a few things, they were placed in the incubator folder until the next major release. This seemed all right to me.

I downloaded the source code for the framework, added the incubator libs and normal libs to the PHP include path (with incubator first, so it takes those in priority) and started playing with it. Actually, I did not get to play too long before I ran into some problems. It seems like the new version relies on some PHP 5.2 only features. There was no warning for this anywhere, or if there was, I did not run into them. Still, this is acceptable. The Zend Framework is meant to be PHP 5 only and should be switching to bleeding edge technology before it gets too large of a user base. Anyway, as a user of *ubuntu, the bundled version of PHP is 5.1.x, so I removed the incubator libs and decided to play with the old versions instead. I was told the changes were mostly internal, so I should be able to live without them and simply switch later.

MVC

I simply followed the documentation to build the skeleton to start from. Everything went very well. Documentation is one of the very strong aspects of the framework. The API documentation is a little poor (as in auto-generated from source code and function headers were also auto-generated), but the end user documentation is great for the most part. Some libraries are under-documented, but I guess this will be resolved in a near future.

Once I had the MVC framework set-up, I decided to build a small form and use some input, just to play with view and such. The Zend_View class is very simple, and yet very powerful. I never really liked template engines like Smarty or others. I think PHP does HTML display good enough, and this is the default behaviour of the View class. It does not do much more than call a PHP script and catch the output. It lets you assign parameters and get them from the template, but it really forces you to separate the display and logic, and suggest good coding practices.


<?php if ($this->books): ?>
    
    <!-- A table of some books. -->
    <table>
        <tr>
            <th>Author</th>
            <th>Title</th>
        </tr>
        
        <?php foreach ($this->books as $key => $val): ?>
        <tr>
            <td><?php echo $this->escape($val['author']) ?></td>
            <td><?php echo $this->escape($val['title']) ?></td>
        </tr>
        <?php endforeach; ?>
        
    </table>
    
<?php else: ?>
    
    <p>There are no books to display.</p>
    
<?php endif; ?>

I took this code sample straight from the documentation. I always liked this alternate syntax when writing conditions or loops in HTML. It simply looks clean. But the very nice part about this template is that a special escape method is defined to escape output. No need to ask yourself which function to use to escape the information. Actually, you can actually still ask yourself that question, because the function used internally by escape can be chosen. I think this is a very good decision on the long run. With all security alerts that come up, if one is actually affecting what ever function is being used, it can actually be redefined to handle a specific case and the entire code base will be fixed.

You probably noticed that the values were accessed as attributes. The Zend_View class allows you to define properties on the fly. I think it simply looks more elegant than calling an assign() method everywhere with a key and value. The view actually does even more. It manages those display helper functions for you. All you have to do is indicate the path where you store your helpers and you will be able to call your own functions in a fashion similar to the call to escape().

Defining the path to controllers, views and helper functions, that makes quite a lot of configuration. It does not matter at all, because all those things can be initialized in your index page, objects can be stored for access using the Zend::registry() function and you no longer need to touch any configuration. This is a very nice aspect of the Zend Framework. Everything is independent. Once you’ve set-up the __autoload() function, you no longer need to worry about including files, paths to scripts or anything. The right controller will be called, the view is managed and everything in between is magic.

Input filtering

After playing with the views, I decided to handle some input. Naturally, I looked at the Zend_Filter_Input class. This was probably the worst decision I made that day. I wasted a whole lot of time to end up figuring out that the testEmail() method was not implemented, which was totally undocumented. I decided to fall back to the ext/filter extension, which was not installed by default in 5.1.x and that I could not get installed using pecl. I had to install 5.2.0, so I did.

0.2.0 incubator

With the incubator issue resolved, I reactivated the include path for it. Bad surprises there. Very bad. Nothing was working any more. Even if most of the changes were actually internal, it seemed like quite a few changes were required in the initialization phase. The worst part was that none of the samples in the incubator documentation had been updated to reflect those changes. I somehow had the feeling that release was pushed out early for ZendCon. So I searched on the web to find some documentation about it. The 0.2.0 version had been released for quite a few days now. Someone, somewhere must have figured out how to solve this. I simply didn’t feel like searching for samples in the unit tests or read too much API documentation to find the changes.

I found this piece of code on some website (I removed some information that could help identify it, because really, it’s a shame it was ever published). The article actually had the mention ‘updated for 0.2.0′, and they seemed to be proud of it. Actually, they might have used the 0.2.0 release without the incubator packages, which really makes MVC the same thing as 0.1.0.

<?php

require_once 'Zend.php';

Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_Controller_RewriteRouter');

$controller = Zend_Controller_Front::getInstance();
$router = new Zend_Controller_RewriteRouter();
$router->setRewriteBase('/');
$controller->setRouter($router);
$controller->setControllerDirectory('../app/controllers');

// View init
Zend::loadClass('Zend_View');
$view = new Zend_View;
$view->setScriptPath('../app/views');
Zend::register('view', $view);

$controller->dispatch();
?>

So, what was wrong about it? Well, it looked pretty much like the code I previously had. I can tell you, that had nothing to do with 0.2.0. The author sure did not try to run the samples. First, Zend_Controller_Front::getInstance() no longer existed. The front controller class was now a simple class that had to be created like any other object. Same for setRewriteBase(), it’s gone.

The other major change in 0.2.0, the path routing is now independent from the controller/action mechanism. In the previous release, a path like http://www.example.com/foo/bar would have called barAction() on the FooController class. Additional parameters could be passed in the URI using /foo/bar/key1/value1/key2/value2/, which is quite ugly. The new routing mechanism allows to define custom paths using a syntax like ‘article/:year/:id’, where elements starting with a colon are variables. Using the previous example, http://www.example.com/article/2006/10 would be a valid URL and send the ‘year’ parameter as 2006 and ‘id’ as 10 to the selected controller/action. So, extra steps are actually required to bring a compatibility layer to the previous version. This is what the code should look like.

<?php

include 'Zend.php';

function __autoload($class)
{
    Zend::loadClass($class);
}

$view = new Zend_View;
$view->setScriptPath('../app/views');

Zend::register('view', $view);

$router = new Zend_Controller_RewriteRouter();
$router->addRoute('compat', new Zend_Controller_Router_Route(
	':controller/:action',
	array( 'controller' => 'index', 'action' => 'index' ) ) );

$front = new Zend_Controller_Front;
$front->setControllerDirectory('../app/controllers');
$front->setRequest( new Zend_Controller_Request_Http(
	'http://www.example.com' . $_SERVER['REQUEST_URI'] ) );
$front->setRouter($router);

$front->dispatch();

?>

I had to spend quite a while reading API documentation, unit tests and bug reports to figure out a routing problem. The line with the setRequest call really should not be required. The framework should find out about it, just like it did in the previous version, but without it, there is no way to get anything routed to anything else than the default controller/action.

Other than this problem with the preview 0.2.0 release, the Zend Framework is really promising. The MVC pattern is highly inspired by Ruby on Rails, there is no doubt about it. In fact, the framework documentation is refering to the RoR documentation in the section about routing. There is a whole lot more to the framework than MVC thought, I will get to other components an other day.

Categories: Programming Tags:

48 hours, 8000 km, 2 chemical checks, 45 minutes speech

I’m currently on my way back home from San José after giving my talk (time of writing, published from the first internet access I could get). I wish I could have spent more time and see the city a little more, but that will be for next time. I spent a total of 25 hours in the hotel, which makes a 1:1 ratio with the total transportation time. I’ll have to remember that jet lag does affect my system.

Overall, the conference was a lot of fun. It sure was a lot more business oriented than PHP Quebec or about any other conference/expo I ever attended to, except maybe GTEC. Everything was sponsored in there: breakfest, wireless access, coffee. I was told there were 550 registered attendees, but obviously, that was counting all speakers and sponsors, because I did not see that many people in the conference rooms. Like most speakers, I just spent time talking, drinking coffee and using the wireless access. I didn’t attend to a single complete session. It was just good to be able to spend time with the other speakers. I met most of them in Montreal over the past conferences, but as an organizer, I was usually too busy to enjoy it.

Considering my talk was a little off topic to the conference and not really oriented towards all attendees, I think it was quite a success. I had quite a lot of competition. Wez was in the room next to me covering PDO, which did attract most of the crowd and it was the last opportunity to get the certification exam. Still, I would say the room had around 25 heads at all time. A few left early in the presentation, more joined. The downside was that I spoke a little too fast and skipped a few elements, but it turned out to be good since it left more time for questions and I had a lot. It allowed me to cover those elements I forgot on the first run and give more examples. With all the questions, I ended up being right on time.
One of the interesting comments I had after the session was by one of the Zend employees. He recommended I used more stories and annecdotes. I had quite a few of them over the presentation, but as a member of the audience, he noticed how people’s attention was caught during those moments. This is something I didn’t notice from the stage, mostly due to that spotlight straight in my eyes. Basically, that thing was so strong I couldn’t see anyone’s reaction unless they started laughing.

Anyway, the interesting part of that comment was that my talk was completely non-technical. I had only three slides that actually had written content about more detailed or “technical” elements. The crowed was composed of PHP developpers. I had nothing even close to code. Still, technical aspects didn’t catch their attention. This comment was straight in line with Daniel Pink’s “A Whole New Mind”. People like to hear stories. They like to hear about situations that are familiar to them. I just wish I had read that book earlier. I was actually reading it during the second half of my preparation period, so it did affect the content, but it could have been so much better.

I’m quite disappointed I had to leave so early after my presentation. I would have loved to hear more comments after the presentation. I had so much trouble with airports on my way to San José that I didn’t want to take any chances for the way back. Now I’m sitting in the airport, past security, with over 2 hours before my flight. I don’t know if it’s the tie giving a better impression than a T-Shirt, but security check went better than I expected.

Overall, I think people enjoyed the presentation. It did change from the usual bullet point presentation, which was pretty much the standard in the rooms I gave a peek. Since this was my first presentation outside Montreal, I’m quite satisfied with the result. I will need to work a little better on topic transitions in the future, I was a little nervous and some of them were cut short. Even I could feel it.

That spotlight sure didn’t help for the stress part. They were actually filming the entire thing. I have no idea what they will be doing with it, but you might get to see them published, for best or worst. It would actually be nice if it were published, quite a few people stopped me in the halls before my presentation. Apparently, estimation is one of those skills most developper would like to improve, but the call for sessions like PDO is just too strong.

Back to the conference in general. I finally understood why speakers like Montreal’s conference so much: Food. The food was not bad, but it really was nothing to what is offered in at PHP Quebec. I had a single lunch meal at the conference, the chicken sandwich was good, but really nothing compared to a buffet including beef, chicken and fish, plus salads and desert. Even for the evening, the conference is holding a (sponsored) happy hour, with free beer and some food. It’s good because it keeps everyone in the expo zone, but I think going out of the hotel and getting people to see a bit of the city is a lot better, even if they have to pay for their beer. I would have loved to see more of San José.

Still, it was a lot of fun and I’m glad the Zend people invited me.

Categories: General Tags: