Fixing The Bloggie Routing
Armed with what we learned we can now create a proper set of routing rules that will remain maintainable when we go on extending the blog we're building. We should create the basic layout for a CRUD-style set of routes as the sample product routes.
So, let's reorganize our routes:
- Create a parent route for all posts that just sets the module. Name it "posts", the pattern should be "^/posts" (remember the anchor on the left) and it should set the module to "Posts". It should be the first route after the index route.
- As we're currently not having a page that displays all posts page by page, we'll go and create the a route named "posts.post" as a child of the posts route. Remember that the concatenation rules for the route name allow you to leave the first part out, so the name attribute will be set to ".post". The pattern should match a post-id, so we'll use ^/(post:\d+). Don't forget the left anchor. This route sets the action to "Post", even though that action does not exist in itself. It will serve as a container for all it's subactions.
- The next route will server for displaying a single post. Create "posts.post.show" as a child of "posts.post". The name attribute is ".show", the pattern ^$ - we want to match the empty string only. Ste the action attribute to ".Show" - it will be appended to the parents route "Posts" to the fully qualified name "Post.Show".
- Remove the old "post" route and the example routes. Your app/config/routing.xml should now look like this:
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0" xmlns="http://agavi.org/agavi/config/parts/routing/1.0">
<ae:configuration>
<routes>
<!-- default action for "/" -->
<route name="index" pattern="^/$" module="Posts" action="%actions.default_action%" />
<route name="posts" pattern="^/posts" module="Posts">
<route name=".post" pattern="^/(post:\d+)" action="Post">
<route name=".show" pattern="^$" action=".Show" />
</route>
</route>
</routes>
</ae:configuration>
</ae:configurations>
Open your browser and point it to http://localhost/bloggie/pub/posts/123 and you should see the stub post page we created earlier on. Note that now you need to provide a post id to access the page, if you leave it out, you'll be directed to the 404 page.
Generating URLs
So far we did not link the posts on the index page to the proper detail page. Generating an url from a route is a simple task, just call AgaviRouting::gen() with the routename, add the parameters you want to set and that's it. Let's fix our IndexSuccessView [app/modules/Posts/views/IndexSuccessView.class.php] to generate the proper urls. We'll read the posts attribute, modify the entries and write it back.
<?php
class Posts_IndexSuccessView extends BlogPostsBaseView
{
public function executeHtml(AgaviRequestDataHolder $rd)
{
$this->setupHtml($rd);
$ro = $this->getContext()->getRouting();
$posts = array();
foreach($this->getAttribute('posts') as $p) {
$p['url'] = $ro->gen('posts.post.show', array('post' => $p['id']));
$posts[] = $p;
}
$this->setAttribute('posts', $posts);
$this->setAttribute('_title', 'Latest Posts');
}
}
?>
Now all posts on the front page link to the appropriate detail page.

