Understanding Kohana Routes

Posted on

A lot of people struggle with creating a simple template system with Kohana Swift Php Framework, but you don’t have to! Today I will explain the basics of controllers and routes so you can finally be productive with Kohana.

Getting Started – Routes

You’ll probably want to start off with a clean install of Kohana, but it isn’t necessary. I will begin by explaining routes and how they could relate to the directory structure and naming conventions of your controllers.

Each controller has a specific name, and they aren’t just chosen at random. They do hold some meaning to them, and with that being said, you should be able to know where the location of the class is just by reading the name of its definition.

Take for example a controller named Controller_Admin_Settings. We can assume that it will be located in our install of Kohana at this location: application/classes/controller/admin/settings.php (See below).

Kohana Controller Directory Structure

Directory Structure

I put my settings controller in the admin folder because I like to organize the areas of my site into “sections”. The admin section would be a separate area of the website obviously not available to normal users. In order to serve up this file in the web browser, we would have to specify a route in our bootstrap file. It would look like this:

Route::set('settings', 'admin/settings(/<action>)')
		'directory' => 'admin',
		'controller' => 'settings',
		'action' => 'index',

Explaining the Route

In order to view this page in a browser, you would need to visit the url yourwebsite.com/admin/settings.
Below illustrates the different parts a route can have:
Kohana Route

The Route pattern is what the url will need to match in order to successfully find the page at that location. The required part of the route is not enclosed inside parenthesis (), whereas the optional part of the url will be inside of nested parenthesis. Any dynamic parts of the url or “segments” need to be enclosed in carrots <>. Each segment enclosed in carrots will then be available in a variable later on, inside your action. The defaults array specifies the parts of the route explicitly. Since my settings controller is in my admin folder, I need to specify the directory as I did above.

This is really about it. There are some more options for creating routes such as defining a regular expression that each segment should match in order to be “valid”. You can read about those here.

I know I like the route system provided in Kohana, but once you fully understand them, they seem extra useful and you will definitely see the power they give you. If you have any questions leave them in the comments.

Thanks, and happy coding! Dont forget to sign up for my new startup This Is Epic

  • http://twitter.com/daGrevis Raitis Stengrevics

    Thanks for this tutorial. I would like to see some more advanced stuff like regexes (as you already mentioned) and, so called, reversed routing (I’m not sure I named it correct).

    • http://zackperdue.com Zack Perdue

      Yeah, I’ll work on getting some more posts on the more advanced areas of the route system.

      I’m glad you found it helpful.

  • Pingback: Kohana Reverse Routing | Zack Perdue()

  • Milen

    Man, you’ve returned my faith in this framework. I was stuck on making some kind of clean working admin + public layout for my app. Your tutorials on routing helped A LOT!

    • http://zackperdue.com Zack Perdue

      It can be tricky to get these concepts down, but once you do, you will see the power they give you. I love Kohana’s routing system.

      • xXxjohannxXx

        kohana is a pile of shit