Using MenuStructure module in KO3
MenuStructure is a simple module I built for Kohana3 for creating menu trees. I'ts very easy to use, and in this tutorial I will show how to build a very basic website with a tree navigation menu.
First of all, start with a clean installation of Kohana 3 (that we will call ko3site), and then setup the database and htaccess file. (You should know how to get to this point).
Now, install MenuStructure in the application's modules folder.
ko3site/modules/menustructure
Activate the module by adding the menustructure line in the bootstrap file.
ko3site/application/bootstrap.php
Look for the modules array.
Kohana::modules(array( // 'auth' => MODPATH.'auth', // Basic authentication // 'cache' => MODPATH.'cache', // Caching with multiple backends // 'codebench' => MODPATH.'codebench', // Benchmarking tool 'database' => MODPATH.'database', // Database access // 'image' => MODPATH.'image', // Image manipulation 'orm' => MODPATH.'orm', // Object Relationship Mapping 'menustructure' => MODPATH.'menustructure', // OAuth authentication // 'pagination' => MODPATH.'pagination', // Paging of results // 'unittest' => MODPATH.'unittest', // Unit testing // 'userguide' => MODPATH.'userguide', // User guide and API documentation ));
So far, we just did what you do with pretty much every module for Kohana.
Now we can start building our site by going to the same bootstrap file and look for the routes section to modify the default one to this:
Route::set('default', '(<id>(/<id2>(/<id3>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
By doing this, we changed the default routing of the site to always look for the index action in the welcome controller. In other words, everything you pass as a URL now will be passed to that action.
NOTE: You can still use other controllers, you just need to set them manually.
Next is creating the database table for MenuStructure. The elements a link should have are listed in the Readme file, but in this case we are using a database.
CREATE TABLE `ko3`.`entries` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `parent_id` INT NOT NULL , `title` VARCHAR( 50 ) NOT NULL , `link` VARCHAR( 50 ) NOT NULL , `body` TEXT NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
Now, create the respective Model file in ko3site/application/classes/model/entry.php
<?php defined('SYSPATH') or die('No direct script access.');
class Model_entry extends ORM {
protected $_table_name = 'entries';
}
Create a view that contains the following:
<?php echo $menu; ?> <h1>content</h1> <?php echo $content; ?>
And finally we need the actual controller.
public function action_index ($seg1 = null, $seg2 = null, $seg3 = null) {
$entries = ORM::factory('entry')->find_all();
// Build the current path. I should build this in a better way though.
$current = '';
$current .= $seg1 ? $seg1 : '';
$current .= $seg2 ? ('/' . $seg2): '';
$current .= $seg3 ? ('/' . $seg3): '';
// Let's get the body of the current page
$body = ORM::factory('entry')->where('link','=', $current)->find()->body;
$options = array('link_prepend' => '', 'current_path' => $current);
$this->request->response = View::Factory('welcome/index')
->set('menu', MenuStructure::factory($entries, $options)->get_menu())
->set('content', $body);
}
That's it. The site should now be rendering the right content in every page.
You should just add some pages in the database to see how it works.
If you have any problems doing it, just let me know on the comments.
latest posts
About
Hi, I'm Ivan Soto Fernandez (yes, two last names). I'm a chilean web developer living in Edmonton, Canada. I'm also an anime fan, Gundams lover and gamer. Welcome to my blog.
You can read more about me or follow me on Twitter or any of the following social websites.