Smutty

MVC Framework

SmuttyRoutes

By default, Smutty dispatches requests based on the assumption that the url is broken up into the controller name, the action name, and the id value respectively. But you can specify custom routes in app.cfg to provide custom (more sensible/useful) URL's to your controllers or their actions. And you can do this by controller or action to limit changes only to that part of the application. Lets go through some examples to demonstrate.

NOTE: When performing routing Smutty will look for the best possible match, so first it will look for a match for the current action, then for the controller, and if nothing is found it'll fall back on the default specification.

By Controller

The first method for specifying a custom route is by controller. This is done by putting the controllers name after the route prefix.

route.post = action/id

This definition means that for the Post controller, the arguments that follow it in the URL will be the action and then the id. This is obviously the default, so won't change anything. But say you did the following...

route.post = action/year/month/day

Now things get a little more interesting. You can see the definition has changed from action/id to action/year/month/day, this means the first argument after the controller name will be the action, then there can be 3 more arguments that will be named year, month and day. These additional arguments will then be available to the action via the $data object.

function showAction( $data, &$session ) {
    $year = $data->int( 'year' );
    $month = $data->int( 'month' );
    // etc...
}

NOTE: action is reserved as it refers (obviously) to the action for the controller. All other definition names are just argument names.

NOTE: As an example you can look at the URL for this wiki. You will see there is a custom route operating so that the action parameter is reversed from it's default, which makes for a prettier and more intuitive URL. The route is defined for the wiki controller as:

route.wiki = name/action

By Action

The last example will effect the controller such that all it's actions will not receive their id argument as normal, but will assume to use the defined year/month/day format. This may be desirable, but if we want to limit the specification to just one particular action then we can do so by including the action to bind the route to in it's definition, like so...

route.post.show = year/month/day

This will then mean that the show action alone will be effected by this new custom route.

Smarty Functions

One really nice helper Smutty provides is that when specifying URL's to Smarty functions like link and url, it will react properly to your custom routes, so their generated URL's will work correctly (dynamically) with your application. Another reason to make use of them!

Wildcards

You can also use config wildcards to specify routes for multiple actions. For instance, to effect all actions that start with view (eg. viewSimple, viewRss, viewXml, etc...) you could use something like...

route.post.view* = name/file
# or even for controllers, it doesn't matter...
route.view* = what/ever

But you could also specify routing information for a specific action if you liked...

route.post.view* = name/file
route.post.viewById = id

Smutty tries to be as flexible (but also simple) as possible.

Links: Post a comment

Posted by Testerkjr on Apr 24

Hellotid - this is just a testing, don't worry about it

Posted by <Name> on Apr 26

Nice webpage, lovely,coolest design.
cialis

Useful Pages

Links