Showing posts with label kohana framework. Show all posts
Showing posts with label kohana framework. Show all posts

Kohana 3 :: Writing A Module

In this tutorial we will cover how to build a module from scratch in Kohana 3. If you aren’t familiar with the Kohana framework then I recommend you read the beginners introduction to Kohana.

The Plan

We will be building a module that replaces Kohana’s own view layer (Kohana_View). It will use the PHP template library Twig. We want our own view layer to be API compatible with Kohana’s view layer. This will ensure that other modules work out of the box and that the only code the developer will need to alter in their application are the templates (so that they are Twig compatible). No code in any controller will need to be modified.
Because Kohana follows HMVC (see beginners introduction to Kohana) we will be calling our classView. This means that all calls in the application to the class of View will be to the View class in our module, not Kohana’s own View class.

Kohana 3 - Archive like route

application/bootstrap.php



Route::set('post', 'post/<year>/<month>/<day>/<title>', array('year'=>'\d{4}', 'month'=>'\d{2}', 'day'=>'\d{2}'))
    ->defaults(array(
            'controller' => 'post',
            'action'     => 'index',
));

Controller function




public function action_index($year, $month, $day, $title){
       //Your code here
}

or
Request::instance()->param('year') ...


Enjoy

A Kohana view template

$view = View::factory('template');
$view
->title = 'My title';
$view
->description = 'An example of my template view.';
$view
->keywords = 'example,view,template';
$view
->css = array(
    
'normal'    => 'style.css',
    
'IE'        => 'ie.css',
);
$view
->js = array(
    
'normal' => 'spark.js',
    
'normal' => 'jquery.js',
);
$this
->request->response = $view->render();



html


<!DOCTYPE html>
<html lang="en">
    
<head>
        
<title><?php if(isset($title)) ? echo $title : echo 'Untitled' ?></title>
        <meta name='description' content='
<?php if(isset($description)) ? echo $description : echo 'No description' ?>' />
        <meta name='keywords' content='
<?php if(isset($keywords)) ? echo $keywords: echo 'No keywords' ?>' />
        
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
        
<?php
            
if(isset($css)) :
            
foreach($css as $type => $file) :
            
if($type != 'normal') echo '<!--[if ' . $type . ']>';
        
?>
        <link type='text/css' href='
<?php echo $file ?>' rel='stylesheet' />
        
<?php
            
if($type != 'normal' echo '<![endif]-->');
            endforeach
;
            endif
;
        
?>
        
<?php
            
if(isset($js)) :
            
foreach($js as $type => $file) :
            
if($type != 'normal') echo '<!--[if ' . $type . ']>';
        
?>
        <script type='text/javascript' src='
<?php echo $file ?>'></script>
        
<?php
            
if($type != 'normal' echo '<![endif]-->');
            endforeach
;
            endif
;
        
?>
    
</head>
   
    
<body>
       
    
</body>
</html>