In the process of sorting out the basics of working with the Kohana PHP framework, the third version, I was faced with the fact that I did not immediately realize how to write a module for this framework. Not everything seemed intuitive, due to not too much experience with frameworks.
The investigation of the modules available in the default installation helped, but not much, I still had to use the scientific method of typing.
Therefore, I decided to write notes about the creation of the module. Suddenly someone will come in handy.
')
So let's get started.
The basic directory structure of any module for Ko3 looks like this:
module_name
|
| --classes
|
| --config
|
--views
It is logical that in the
classes directory there will be classes needed for the module to work. In the
config directory - accordingly the file with the module configuration. Well, in the
views directory - display templates.
For example, let's write some simple module. Let it not be of great practical use, but the main thing for us is to understand the principle of work.
Let's write a header output module. As data, we will pass a string, and at the output we expect it to be drawn up in some form.
Create the
tester directory in the
modules directory, this will be the name of our module. In it we will create directories
classes, config, views .
Let our module in the configuration store:
-font size, which will be displayed header;
- header color;
Create a tester.php file in the config directory (taking an example with default modules in Ko3, the file names will be the same as the module name), with the following contents:
<?php
return array(
'default' => array(
'size' => '20' ,
'color' => '#ff0000' ,
'view' => 'tester/view.php' ,
),
);
In the
view element specify the output template of our headers. And of course, we need to create this template in the
views directory of our module. It will look something like this:
| --views
|
--tester
|
--view.php
Template content will be simple:
<div style="clear: both; font-weight: bold;">
<font style="color: <?php echo $fontColor ?> ; font-size: <?php echo $fontSize ?> pt;"> <?php echo $text ?> </font>
</div>
And now let's move on to the writing of our class, which will create all this. In the
classes directory, create a file
tester.php (yes, exactly with the same name) with the following contents:
<?php
class Tester
{
// font size of our header
protected $fontSize ;
// color of our header
protected $fontColor ;
// text of our header
protected $text ;
// merged configuration settings
protected $config = array();
public static function factory ( $text )
{
return new Tester ( $text );
}
public function __construct ( $text )
{
// Load config file
$config_file = Kohana :: config ( 'tester' );
$this -> config = $config_file -> default ;
$this -> fontColor = $this -> config [ 'fontColor' ];
$this -> fontSize = $this -> config [ 'fontSize' ];
$this -> text = $text ;
}
public function render ()
{
// Load the view file and pass on the whole Pagination object
return View :: factory ( $this -> config [ 'view' ], get_object_vars ( $this ))-> render ();
}
}
Pay attention to the constructor, it loads the configuration file, and sets the properties of the object that we need to display the header.
We will process our headers directly in templates, as follows:
<?php echo Tester :: factory ( 'New Module Header' )-> render (); ?>
Now we need to connect our module in the
bootstrap.php file.
Open, find the next section:
Kohana :: modules (array(
// 'auth' => MODPATH.'auth', // Basic authentication
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
// 'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'pagination' => MODPATH.'pagination', // Paging of results
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
and add to this list a line with our module:
Kohana :: modules (array(
// 'auth' => MODPATH.'auth', // Basic authentication
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
// 'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'pagination' => MODPATH.'pagination', // Paging of results
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
'tester' => MODPATH . 'tester' ,
));
The total directory structure of the module:
tester
|
| --classes
| |
| --tester.php // class of our module
|
| --config
| |
| --tester.php // configuration file
|
--views
|
--tester
|
--view.php // display template
That's all, after we have registered our module in
bootstrap.php we can use new headers in the templates.
Naturally, this functionality is, firstly, very limited, and secondly, it would be simpler and more logical to write a method in the
HTML helper class. But, nevertheless, the principle of organizing the modules, I think, is understandable.
If you need a module that could act as a controller for the query string (for example, it will produce a result when you request
site / module / method / param1 / param2 ), you first need to create a subdirectory controller in the
classes directory and create a file in it with the class of our controller. Secondly, it will be necessary not only to connect the module in the
bootstrap.php file, but also it will be necessary to specify the route (
route ) in the routes section, to handle the URL pointing to our module.