How to write a simple module for Kohana 3?

    In the process of understanding the basics of working with the PHP framework, Kohana, the third version, was faced with the fact that it was far from immediately understood how to write a module for this framework. Not everything seemed intuitive, due to not too much experience working with frameworks.
    The study of the modules available in the default installation helped, but not much, anyway, I had to use the scientific poking method.

    Therefore, I decided to write notes about creating the module. Suddenly someone will come in handy.



    So let's get started.

    The basic directory structure of any module for Ko3 looks something like this:

    module_name
    |
    | --classes
    |
    | --config
    |
     --views
    


    It is logical that the classes directory will contain the classes necessary for the module to work. In the config directory , respectively, the module configuration file. Well, in the views directory - display templates.

    For an example we will write some simple module. It may 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. We will pass the string as data, and at the output we expect it to be issued in some form.

    Create the tester directory in the modules directory , this will be the name of our module. In it, create the classes, config, views directories . Let our module in the configuration store:


    -font size by which the title will be displayed;
    The color of the title

    create the tester.php file in the config directory (taking an example with default modules in Ko3, make the file names the same as the module name), with the following contents:


    return array(

        'default' => array(
            'size'    => '20',
            'color'    => '#ff0000',
            'view'    => 'tester/view.php',
        ),

    );



    In the view element, specify the output template for 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
    


    The contents of the template will be simple:


        ; font-size: pt;">  



    And now we pass actually to writing our class, which will create all this. In the classes directory, create the tester.php file (yes, with exactly the same name) with the following contents:


    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, the configuration file is loaded in it, and the properties of the object that we need to display the header are set.
    We will process our headers directly in the templates, as follows:

    render(); ?>



    Now we need to connect our module in the bootstrap.php file.
    Open, find the following 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 a line with our module to this list:

    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 final 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 registered our module in bootstrap.php we can use the new headers in the templates.

    Naturally, this functionality is, firstly, very limited, and secondly, it would be easier and more logical to write a method in the HTML helper class . But, nevertheless, the principle of organizing 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 querying site / module / method / param1 / param2 ), you will first need to create a controller subdirectory in the classes directory and create a file in it with the class of our controller. Secondly, you will not only need to include the module in the bootstrap.php fileBut will also need to specify a route ( route ) in the routing section for processing URL pointing to our module.

    Also popular now: