German book recommendation: "Das Zend Framework"

Permanent Link: German book recommendation: 4. April 2009 Comment No Comment

In case you're planning to work or already do work with the Zend Framework, there's a really good (german) book that just got out, which i test-read: Das Zend Framework: Von den Grundlagen bis zur fertigen Anwendung by Ralf Eggert.

Although this book is primarily for Zend Framework beginners, there is also a lot of content you may still not know even though you already work with ZF a bit longer. The code examples are really good and helpful and you really get to know a lot about ZF. The book shows how to build a web application step by step using ZF.

You can order the book at Amazon.

Zend Framework: Generating URLs from defined routes

Permanent Link: Zend Framework: Generating URLs from defined routes 23. Februar 2009 Comment No Comment

The Zend Controller Router allows you to easily create your own routes. That is very useful, but it seems rather unknown, that you can also use these routes to generate URLs according to their definition.

Let's assume you have a route like this:

$controller = Zend_Controller_Front::getInstance();
$router = $controller->getRouter();

$productRoute = new Zend_Controller_Router_Route(
':pid/:name',
array(
'module' => 'default',
'controller' => 'product',
'action' => 'show'
),
array(
'pid' => '\d+'
)
);
$router->addRoute('productpage', $productRoute);

In this route we parse a URL that is supposed to show a product page. The URL will look like http://www.phpdevblog.net/4815162342/Microsoft+Windows+XP+Professional for example. The Router would explode it into pid=4815162342 and name="Microsoft Windows XP Professional"

To generate a URL with the help of the Router you simply need to call the assemble method, which you can either call from the router or the definied route, but keep in mind that you will get different results!

From the router the assemble would look like this:

$link = $router->assemble(
array(
'pid' => '4815162342',
'name' => 'Microsoft Windows XP Professional'
),
'productpage'
);

The result of this method is /4815162342/Microsoft+Windows+XP+Professional

The only difference, if you call the assemble method from the defined route, is that you don't need the route name in the second parameter:

$link = $productRoute->assemble(
array(
'pid' => '4815162342',
'name' => 'Microsoft Windows XP Professional'
)
);

Here the result is 4815162342/Microsoft Windows XP Professional