DealTaker.com > Inside DealTaker > Kohana PHP 3.0 (KO3) Tutorial Part 6

Inside DealTaker

Kohana PHP 3.0 (KO3) Tutorial Part 6

March 3, 2010 ellisgl

Welcome to the sixth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven’t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over Routes and Routing.

What is Routing? Well Routing is nothing more than taking a request, looking at it and directing it to the right place. Think of it Kohana’s version of the .htaccess file. So why would you want to use routing you might ask. Well there are so many reasons why you would want to use routing, so I’m not going to over all of them, but give you a couple examples of how to setup routes for different scenarios.

So what does a route look like? Open up “bootstrap.php” located in “application/”, and scroll down to the comment that says:
[php]/**
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/[/php]
The next thing you should see is something like this:
[php]Route::set(‘default’, ‘(<controller>(/<action>(/<id>)))’)
->defaults(array(‘controller’ => ‘welcome’,
‘action’ => ‘index’));[/php]
So we have a route setup initially for us, but what does this tell us. If we break it down we get:
1. Set a route with the name “default”
2. ‘((/(/)))’ Tells the route the controller, action and id are optional. The parentheses are containers that denote if an option is opional or not and the less / greater than symbols tell the the router to assign the value to the variable of the value in-between the less / greater than symbols.
3. The default values for the controller is “welcome” and the default value for the action is “index”

So when we go to “http://yourserver/mykohana3/”, the router see that we have not set a controller nor an action in our URL. It will go through the route rules that we have set to find a match. In our case, we have a default route, which is named “default”. This default route will replace the missing controller with “welcome” and the action with “index” and the rest of the system will process it. Simple so far?

If we were to to “http://yourserver/mykohana3/hmvc/”, the router would notice that the controller has been set, but not the action. The router then assign the default action value of “index” for us.

You might have noticed the optional “id” in the route. This I would say is pretty commonly used and we could pass an ID on to our action. Example:
“http://yourserver/mykohana3/hmvc/index/111″. Of course this example will not do anything special since we didn’t set up any logic to handle it. Now what if we browsed to “http://yourserver/mykohana3/hmvc/index/111/222″? We get an error. This is because our router rule is strict and says we can only route for upwards of 3 segments (action/controller/id) and the /222 creates a fourth. To over come this we can though in a overflow handler rule into our default route.
[php]Route::set(‘default’, ‘(<controller>(/<action>(/<id>(/<overflow>))))’, array(‘overflow’ => ‘.*?’))
->defaults(array(‘controller’ => ‘welcome’,
‘action’ => ‘index’));[/php]
If you save the ‘bootstrap.php’ file and reload, you should not see an error now.

You might notice that we’ve added in an extra array “array(‘overflow’ => ‘.*?’)”. This basically sets up a REGEX rules for the “overflow” option which tell the router to capture everything, including slashes and pass it as our “overflow” argument. So if slashes are captured, the router will no longer try to split out the options on slashes.

Lets go back to this “id” option we see in our route. Well, anything we put in that area will be passed to our action. If our id’s are integers, we might want to error out if we pass a non integer. One way we can do this is with the route:
[php]Route::set(‘default’, ‘(<controller>(/<action>(/<id>(/<overflow>))))’, array(‘id’ => ‘[[:digit:]]{1,}’, ‘overflow’ => ‘.*?’))
->defaults(array(‘controller’ => ‘welcome’,
‘action’ => ‘index’));[/php]
So now if you were go to: “http://yourserver/mykohana3/hmvc/index/xxx”, you would get an error about not being able to find the route, which should translate to a 404 in a production environment. Of course you could check the id for an integer inside the action action and decide how to handle it.

Now lets create a route that isn’t our default route. Add the following to the “bootstrap.php” before the default route:
[php]Route::set(‘monkeys’, ‘monkeys(/<action>(/<id>))’)
->defaults(array(‘controller’ => ‘ko3′,
‘action’ => ‘posts’));[/php]
Now if you were open up “http://yourserver/mykohana3/monkeys”, you would end up with the “posts” action of the “ko3″ controller. If you notice, the “monkeys” in the controller spot is not enclosed by parentheses, meaning that is not optional. So the router see “monkeys” in the controller spot and triggers this rule set.

Here’s an example you might want to use to serve up some static pages (Totally stole this from the Unofficial Kohana 3.0 Wiki):
[php]Route::set(‘static’, ‘<page>’, array(‘page’ => ‘about|faq|locations’))
->defaults(array(‘controller’ => ‘page’,
‘action’ => ‘static’));[/php]
There is a bit more you could do with routes, but the above should cover the majority of things you would want to use route for.

Until next time when we will go over modules and helpers, happy coding.
Sources used: Unofficial Kohana 3 Wiki

<<Part 5 | Part 7>>

Post a New Comment

Comments and Reviews:

  1.  
    Wow, I now unders­tand a bit more on how much I need to learn more about Routes­! Amazin­g... Thanks­ for gettin­g back to me on my Tutori­al #5!
  2.  
    @August­: Not a proble­m. I think I will come back to route later on, to be specif­ic, I8N suppor­t.
  3.  
    i have been lookin­g all around­ the intern­et for some good Kohana­ tutori­als and these are by far the best yet i think anyway­s. I just wish there was some tutori­als on the auth and ORM module­s
  4.  
    Lilbig­: Before­ gettin­g to ORM, I'm waitin­g for all the issues­ to be ironed­ out with ORM. For auth, I'll be lookin­g at it at a future­ date.
  5.  
    "Welcom­e to the fifth part". Isn't this the sixth part? Either­ way: I love these tutori­als!
  6.  
    Svish: Oppps. Fixed.
  7.  
    Hello! Do you know or have any e-book regard­ing Kohana­ v3? Thanks­
  8.  
    and by the way, congra­tulati­ons for the kohana­ post-series­ :)
  9.  
    Thaks a lot. Are you plan to write about Valida­te And Upload­? I'm new to Kohana­.
  10.  
    @Elz: Yes I do plan to go over form in the future­.
  11.  
    well thank you for these great tutori­als when do you write the next tutori­al?
  12.  
    Thank you ellisg­l for tutori­als shared­. I go ease and withou­t any proble­m becaus­e they are very clear :). Good luck still!
  13.  
    @SnowT: There is a part 7 avail: http://www.dealta­ker.com/blog/2010/03/26/kohana­-php-3-0-ko3-tutori­al-part-7/ As for part 8, probab­ly in 2 weeks.
  14.  
    hello, I'm new to the commun­ity, the best tutori­al, I have a concer­n that if a user (malici­ous), enter a driver­ that does not exist, you can contro­l.
  15.  
    nothin­g more to say than: thank you for these outsta­nding tutori­als!
  16.  
    I had a hard time with the custom­ routes­ apart from the defaul­t one. The trick was to put the custom­ Route::set(...) BEFORE­ the Route:set('defaul­t',...). It took me quite a while to figure­ it out.
  17.  
    @Goerge­: I'm glad you figure­d it out.
  18.  
    Can I define­ a route like http://yourse­rver/mykoha­na3/monkey­s/123 (Contro­ller/id) while having­ action­ as option­al? I just want to avoid writin­g it like http://yourse­rver/mykoha­na3/monkey­s/index/123 thanks­
  19.  
    good kohana­ tutori­al, would be nice to correc­t the render­ing of [PHP] blocks­. regard­s.
© 2012 DealTaker Inc., a Media General company. All Rights Reserved.

Coupons, Coupon Codes & Promotional Codes