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

Kohana 3 :: alternate 404 method

controller

<?php defined('SYSPATH') or die('No direct script access.');

if ( ! $this->_auth->logged_in('login'))
{
 throw new Kohana_Exception('You are not allowed to view this resource.');
}

Kohana 3 :: 8 helpers

http://pastie.org/981498
http://pastie.org/981501
http://pastie.org/981502
http://pastie.org/981503
http://pastie.org/981504
http://pastie.org/981506
http://pastie.org/981507
http://pastie.org/981508


Enjoy!

Kohana 3 :: Kohana Date helper

< ?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 * Extend the Kohana Date helper
 */
class Date extends Kohana_Date {

 /**
  * Number of months in a year. Value will hold month name
  * 
  * @static
  * @uses    Date::hours
  * @return  array  Array from 1-12 with month names
  */
 public static function months_with_name()
 {
  $months = Date::hours();

  for ($i = 1; $i <= 12; $i++)
  {
   $timestamp = mktime(0, 0, 0, $i, 1, 2005);
   $months[$i] = date("M", $timestamp);
  }

  return $months;
 }

 /**
  * Checks whether a string is a date
  *
  * @static
  * @param  string date
  * @return bool
  */
 public static function is_date($str)
 {
  return (boolean) strtotime($str);
 }

}

// End Date

Kohana 3 :: blog article unique slug

Post_Model
public function save()
{
 $this->slug = $this->_unique_slug(url::title(empty($this->slug) ? $this->title : $this->slug));
 return parent::save();
}

private function _unique_slug($str)
{
 static $i;
 
 $original = $str;
 
 while ($post = ORM::factory('post', $str) AND $post->loaded AND $post->id !== $this->id)
 {
  $str = $original.$i;
  $i++;
 }
 
 return $str;
}

http://forum.kohanaframework.org/discussion/comment/17756/#Comment_17756

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>