**NOTE** I have recently found a much cooler way of doing this – please read this also: http://v3.kohanaphp.com/guide/tutorials.urls .
I have found the routing in Kohana to be one of the most helpful and easiest features with wich to work. In this tutorial I will show you how to use Routes to organize your Public site with your Back-End or Admin section of your site.
Step 1 : Controllers
To help us organize our Admin and Public Controllers we create two folders:
- application/classes/controller/admin
- application/classes/controller/public
Using this folder structure will determine your class names, for example an Admin Users Controller like this:
class Controller_Admin_Users extends Controller_Template
{
/// class code
}
To access this controller you would need a url like: www.mysite.com/admin_users/login
but who wants that ugly thing. It would be much nicer to have : www.mysite.com/admin/users
and we can easily achieve that with Routes.
Step 2
Open the file application/boostrap.php and look for the Route section, which should have something like this:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
This is the default route, so we are going to leave it alone for now and place all of our other Routes above it.
Step 3
We need to create and initial route for the admin home or welcome page. For this example, I’ll use a Home Controller located at application/classes/controller/admin/home.php. That is where I want people to go when they type www.mysite.com/admin. I do this by adding this route:
Route::set('admin', 'admin')
->defaults(array(
'controller' => 'admin_home',
'action' => 'index',
));
Step 4
When someone simply trys to access the root of the site (www.mysite.com), I want them to go to the public home controller located at application/classes/controller/public/home.php. So I just modify the Route that was there when I first intalled Kohana to this:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'public_home',
'action' => 'index',
));
Step 5
Now I want to add a route for that Admin Users controller located at application/classes/controller/admin/users.php, but I want that controller to have other Actions than just the index action, and I want to pass a variable in the url, so I simple do this:
Route::set('admin/users', 'admin/users(/<action>(/<id>))')
->defaults(array(
'controller' => 'admin_users',
'action' => 'index',
));
And in that you have a model to build the rest of your controllers on..