Zend Framework 0.2, Incubator, RewriteRouter and FontController
After time away from the Zend Framework, I dove back in yesterday to play around with 0.2 version. I decided to use the incubator, as there as some fairly significant changes in the works, and I wanted to see what was in store for later releases. By setting your include path to check the incubator directory first in your index.php file, you can make use of the newer classes:
<?php
set_include_path(‘/__shared/incubator/library’ . PATH_SEPARATOR . ‘/__shared/lib’);
?>
I copied over the general structure from another site that was using v0.1.5 and immediately ran into problems with the controller. Using the incubator means I can’t rely on the manual, so I searched through some emails from the Zend mailing list and found some new code instantiating the FrontController and RewriteRouter (FYI - the emails are archived, and this is an extremely useful resource to search if you are having trouble and can’t find the answers in the manual).
I hadn’t used the RewriteRouter before and I must say it is nice. Before, I was employing hackish code to simplify my urls, like
<?php
//for url www.example.com/article/view/article-about-zend
$params = $this->_getAllParams();
$url = key($params); //$url is now ’article-about-zend’
?>
Now, I can use the RewriteRouter in index.php to simplify my urls and code:
<?php
//for url www.example.com/article/article-about-zend
$router = new Zend_Controller_RewriteRouter();
$article = new Zend_Controller_Router_Route(
“article/:url”,
array( “url” => null,
“controller”=>“article”,
“action”=>“view”
)
);
$router->addRoute(“article”,$article);
?>
And in my controller method (article controller, view method per the code above), grabbing the params is easy, because the “:url” sets it up:
<?php
$url = $this->_getParam(‘url’);
?>
I still have some questions about the RewriteRouter, but I’m sure I’ll figure them out as I continue to use it. There are a bunch of other neat classes I am playing with right now, like the caching and logging. I am really impressed with the framework so far and look forward to using other parts of it!