Create Your Own Kohana Module

jsandahl

Kohana surprises once again with its simplicity in working with the code. Let’s say you have a framework or class and you want to make it load on every page within your Kohana application. Regularly, you’d include the files on each page with a line like this:

require_once('../tcpdf.php');

Here, we’re dealing with a .pdf creator class called tcpdf which enables the creation of .pdf files. To turn it into a Kohana module means that we won’t have to include the tcpdf.php file on every page and remember the directory it’s in and provide a relative link to that directory.

To start, you’ll need to create an init.php file and place it in the root folder of your class. My init.php file looks like this:

<?php
require_once('../config/lang/eng.php');
require_once('../tcpdf.php');
?>

Then place your entire class folder in the /modules directory of your Kohana project. There is only one more step to creating your first simple Kohana Module.

Enter your bootstrap.php (/application/boostrap.php) and add a line under your modules array:

'tcpdf'     => MODPATH.'tcpdf', // Creates pdfs

It’s done. Now you can call any method of the class without needing to require the class on every page.

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *