kohana 3.2 tutorial

Kohana is discontinued. I’m now using PhalconPHP / Laravel, but any framework supported by HHVM is a good choice. RIP Kohana, it was nice to do business with you…


Below you will find an article / tutorial on Kohana 3.2 – An elegant HMVC PHP5 framework that provides a rich set of components for building web applications.

I am sharing a site template that use authentication & internationalization. You can download it from github. Any pull request will be more than welcome!

Download via Github:
https://github.com/patricksebastien/kohana-3.2-example


Topics



Install

Using GIT:
http://kohanaframework.org/3.2/guide/kohana/tutorials/git
or
Download:
http://kohanaframework.org/download


Structure of folders:
www/yoursite/site/index.php & .htaccess -> and your assets (css, images, js)
www/yoursite/application -> the very core of your site
www/yoursite/module & system -> core of kohana
or:
www/kohana/version/system & module -> multiple site using kohana
or:
everything in www/yoursite/site (application, module, system, index.php, .htaccess, …)


Test:
127.0.0.1/
If it’s greenish, then remove install.php


index.php
Reflect this structure in www/yoursite/site/index.php:

[ccnw_php width=”550″ theme=”mac-classic”]
$application = ‘../application’;
$modules = ‘../modules’;
$system = ‘../system’;
[/ccnw_php]


bootstrap.php
Modify application/bootstrap.php
[ccnw_php width=”550″ theme=”mac-classic”]
date_default_timezone_set(‘America/Montreal’);
[/ccnw_php]

Set PRODUCTION vs DEVELOPMENT
[ccnw_php width=”550″ theme=”mac-classic”]
if (isset($_SERVER[‘KOHANA_ENV’])) {
Kohana::$environment = constant(‘Kohana::’.strtoupper($_SERVER[‘KOHANA_ENV’]));
} else {
Kohana::$environment = ($_SERVER[‘REMOTE_ADDR’] == ‘127.0.0.1’ ? Kohana::DEVELOPMENT : Kohana::PRODUCTION);
}
[/ccnw_php]

Initialize Kohana
[ccnw_php width=”550″ theme=”mac-classic”]
‘base_url’ => ‘/’, // or for example: /yoursite/site/mykoapp
‘index_file’ => FALSE, // SEO (avoid index.php/mycontroller/action)
‘profile’ => (Kohana::$environment !== Kohana::PRODUCTION), //see how good you are
‘caching’ => (Kohana::$environment === Kohana::PRODUCTION),
‘errors’ => TRUE, //for custom 404, 500 FALSE for internal error handling
[/ccnw_php]

Enable modules (for example):
[ccnw_php width=”550″ theme=”mac-classic”]
‘auth’ => MODPATH.’auth’, // Basic authentication
‘database’ => MODPATH.’database’, // Database access
‘orm’ => MODPATH.’orm’, // Object Relationship Mapping
[/ccnw_php]

Many modules are available:
https://github.com/kolanos/kohana-universe
http://kohana-modules.com/

Set the routes (default controller will be login.php in this example)
http://kohanaframework.org/3.2/guide/kohana/routing

The order of your routes are important!

[ccnw_php width=”550″ theme=”mac-classic”]
Route::set(‘default’, ‘((/(/)))’)
->defaults(array(
‘controller’ => ‘login’, // application/classes/controller/login.php
‘action’ => ‘index’,
));
[/ccnw_php]


.htaccess
Add this line at the very top of .htaccess (protect from sniffing directory)
[ccnw_php width=”550″ theme=”mac-classic”]
Options All -Indexes -Multiviews
[/ccnw_php]
and modify (/ or for example: /yoursite/site/mykoapp)
[ccnw_php width=”550″ theme=”mac-classic”]
# Installation directory
RewriteBase /
[/ccnw_php]

 
Template
http://kerkness.ca/kowiki/doku.php?id=template-site:create_the_template

Create the template controller (classes/controller/template/website.php) extending Controller_Template
[ccnw_php width=”550″ theme=”mac-classic”]
auto_render) {
// Initialize empty values
$this->template->title = ”;
$this->template->content = ”;
$this->template->styles = array();
$this->template->scripts = array();
}
}

/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*/
public function after() {
if ($this->auto_render) {
$styles = array(
‘assets/css/website.css’ => ‘screen, projection’,
);
$scripts = array(
‘http://code.jquery.com/jquery.min.js’,
);
$this->template->styles = array_merge( $this->template->styles, $styles );
$this->template->scripts = array_merge( $this->template->scripts, $scripts );
}
parent::after();
}
}
[/ccnw_php]

Create the controller (classes/controller/login.php) extending Controller_Template_Website (look at the second action for an example on how to use another template per action):
[ccnw_php width=”550″ theme=”mac-classic”]
class Controller_Login extends Controller_Template_Website {

public function action_index()
{
$this->template->title = ‘Log in’;
$this->template->content = View::factory(‘login’); // application/views/login.php
}

// this action is using another template but using the same Controller_Template_Website
public function action_showinfooverlay()
{
$this->template = ‘template/overlay’;
parent::before();
$this->template->title = ‘Log in’;
$this->template->content = View::factory(‘login’); // application/views/login.php
}
}
[/ccnw_php]

Create the html template (view/template/website.php)
[ccnw_php width=”550″ theme=”mac-classic”]




[/ccnw_php]

Finally create your view content application/views/login.php
[ccnw_php width=”550″ theme=”mac-classic”]


[/ccnw_php]

At this point you can point your browser to see the login page:
http://localhost/ -> depending on base_url and .htaccess (could be in a sub-folder)
http://localhost/login -> not defined login as the default controller in bootstrap.php

Someone on #kohana (irc / freenode) made a suggestion of using view classes instead of template controller. Here’s two solutions: https://github.com/zombor/kostache & https://github.com/beautiful/view

 
Configure
Database
Copy modules/database/config/database.php to application/config/database.php
[ccnw_php width=”550″ theme=”mac-classic”]
‘default’ => array
(
‘type’ => ‘mysql’,
‘connection’ => array(
‘hostname’ => ‘localhost’,
‘database’ => ‘yourdb’,
‘username’ => ‘user’,
‘password’ => ‘pwd’,
‘persistent’ => FALSE,
),
‘table_prefix’ => ”,
‘charset’ => ‘utf8’,
‘caching’ => FALSE,
‘profiling’ => FALSE, // if you use profiling turn this on (to see querys)
),
[/ccnw_php]


Cookie
in application/bootstrap.php
must be after: spl_autoload_register(array(‘Kohana’, ‘auto_load’));
[ccnw_php width=”550″ theme=”mac-classic”]
/**
* Cookie
*/
// Set the magic salt to add to a cookie
Cookie::$salt = ‘fjsdijeihrewhbfsugfuyegwufewgwb’;
// Set the number of seconds before a cookie expires
Cookie::$expiration = DATE::WEEK; // by default until the browser close
// Restrict the path that the cookie is available to
//Cookie::$path = ‘/’;
// Restrict the domain that the cookie is available to
//Cookie::$domain = ‘www.mydomain.com’;
// Only transmit cookies over secure connections
//Cookie::$secure = TRUE;
// Only transmit cookies over HTTP, disabling Javascript access
//Cookie::$httponly = TRUE;
[/ccnw_php]


Session (stored in database)
http://kohanaframework.org/3.2/guide/kohana/sessions
in application/bootstrap.php add the default session handler:
[ccnw_php width=”550″ theme=”mac-classic”]
Session::$default = ‘database’;
[/ccnw_php]
Copy system/config/encrypt.php to application/config/encrypt.php
[ccnw_php width=”550″ theme=”mac-classic”]
return array(

‘default’ => array(
‘key’ => ‘fjdsjkfdskjfurew’,
‘cipher’ => MCRYPT_RIJNDAEL_128,
‘mode’ => MCRYPT_MODE_NOFB,
),

);
[/ccnw_php]
Create a table if you want to use database session
[ccnw_php width=”550″ theme=”mac-classic”]
CREATE TABLE `sessions` (
`session_id` VARCHAR(24) NOT NULL,
`last_active` INT UNSIGNED NOT NULL,
`contents` TEXT NOT NULL,
PRIMARY KEY (`session_id`),
INDEX (`last_active`)
) ENGINE = MYISAM;
[/ccnw_php]
Copy system/config/session.php to application/config/session.php
[ccnw_php width=”550″ theme=”mac-classic”]
return array(
‘database’ => array(
‘name’ => ‘session’,
‘encrypted’ => TRUE, // need a key in config/encrypt.php
‘lifetime’ => DATE::HOUR, // 0 = expire when the browser close
‘group’ => ‘default’,
‘table’ => ‘sessions’,
‘columns’ => array(
‘session_id’ => ‘session_id’,
‘last_active’ => ‘last_active’,
‘contents’ => ‘contents’
),
‘gc’ => 500,
),
);
[/ccnw_php]
Use it in your controller:
[ccnw_php width=”550″ theme=”mac-classic”]
Session::instance()->set(‘key’, ‘value’);
Session::instance()->get(‘key’);
[/ccnw_php]


yoursite
Create a file in application/config/yoursite.php
[ccnw_php width=”550″ theme=”mac-classic”]
‘fjdsjkfdskjfurew’,
‘playlists’ => array
(
1478363 => 10171,
22857234 => 10171,
386 => 10171,
722 => 10171,
893 => 10171,
237 => 10171,
),

);
[/ccnw_php]
Then you can call a config like this:
[ccnw_php width=”550″ theme=”mac-classic”]
$playlists = Kohana::$config->load(‘yoursite.playlists’);
Kohana::$config->load(‘yoursite.myconfig1 ‘);
[/ccnw_php]


Message
(might be better to use I18N directly)

Create a file in application/message/yoursite.php for you project
[ccnw_php width=”550″ theme=”mac-classic”]
‘You don\’t have the permission’,
‘wrong’ => ‘Wrong username or password’,
);
[/ccnw_php]
Then you can use it like this:
[ccnw_php width=”550″ theme=”mac-classic”]
Kohana::message(‘yoursite’, ‘permission’);
[/ccnw_php]


Translation
http://blog.mixu.net/2010/11/11/kohana-3-i18n-tutorial/
[ccnw_php width=”550″ theme=”mac-classic”]
‘gfdgfdg’, ‘:user’ => ‘gfdgfd’)); ?>
[/ccnw_php]

 
Validation
http://kohanaframework.org/3.2/guide/kohana/security/validation
Copy system/messages/validation to application/message/validation.php if you want to change the error message
[ccnw_php width=”550″ theme=”mac-classic”]
// Validate a form ($_POST)
if (isset($_POST) && Valid::not_empty($_POST)) {
// Validate the login form
$post = Validation::factory($_POST)
->rule(‘username’, ‘not_empty’)
->rule(‘username’, ‘regex’, array(‘:value’, ‘/^[a-z_.]++$/iD’))
->rule(‘password’, ‘not_empty’)
->rule(‘password’, ‘min_length’, array(‘:value’, 3));

// If the form is valid and the username and password matches
if ($post->check()) {
echo ‘Validated’;
}
[/ccnw_php]

Using a callback for custom validation & error message
[ccnw_php width=”550″ theme=”mac-classic”]
// form post handling
if (isset($_POST) && Valid::not_empty($_POST)) {
// validate
$post = Validation::factory($_POST)
->rule(‘username’, ‘alpha_numeric’)
->rule(‘password’, array($this, ‘pwdneusr’), array(‘:validation’, ‘:field’, ‘username’));
if ($post->check()) {
}
}
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// CALLBACK
// validation rule: password != username
public function pwdneusr($validation, $password, $username)
{
if ($validation[$password] === $validation[$username])
{
$validation->error($password, ‘pwdneusr’);
}
}
[/ccnw_php]

 
Error page

http://kohanaframework.org/3.2/guide/kohana/tutorials/error-pages

Create the views
views/error/404.php / 500.php etc…

Extend the exception handler of Kohana
classes/kohana/exception.php
[ccnw_php width=”550″ theme=”mac-classic”]
add(Log::ERROR, parent::text($e));

$attributes = array
(
‘controller’ => ‘error’,
‘action’ => 500,
‘message’ => rawurlencode($e->getMessage())
);

if ($e instanceof HTTP_Exception)
{
$attributes[‘action’] = $e->getCode();
}

// Error sub-request.
echo Request::factory(Route::get(‘error’)->uri($attributes))
->execute()
->send_headers()
->body();
}
catch (Exception $e)
{
// Clean the output buffer if one exists
ob_get_level() and ob_clean();

// Display the exception text
echo parent::text($e);

// Exit with an error status
exit(1);
}
}
}
}
[/ccnw_php]

Create the controller:
classes/controller/error.php
[ccnw_php width=”550″ theme=”mac-classic”]
request->param(‘message’))) {
$this->template->message = $message;
}
} else {
$this->request->action(404);
}
$this->response->status((int) $this->request->action());
}
public function action_404()
{
$this->template->title = ‘404 Not Found’;
$this->template->content = View::factory(‘error/404’ );
}
public function action_500()
{
$this->template->title = ‘Internal Server Error’;
$this->template->content = View::factory(‘error/500’ );
}
public function action_503()
{
$this->template->title = ‘Maintenance Mode’;
$this->template->content = View::factory(‘error/503’ );
}
}
[/ccnw_php]

Edit application/bootstrap.php to add the route:
[ccnw_php width=”550″ theme=”mac-classic”]
Route::set(‘error’, ‘error/(/)’, array(‘action’ => ‘[0-9]++’, ‘message’ => ‘.+’))
->defaults(array(
‘controller’ => ‘error’
));
[/ccnw_php]

 
Authentication
Copy modules/auth/config/auth.php to application/config/auth.php
[ccnw_php width=”550″ theme=”mac-classic”]
return array(
‘driver’ => ‘orm’,
‘hash_method’ => ‘sha256’,
‘hash_key’ => ‘wigbble’,
‘lifetime’ => Date::HOUR * 2,
‘session_key’ => ‘auth_user’,
);
[/ccnw_php]

Schema for mysql / postgresql located:
modules/orm/auth-schema-mysql.sql

Change the rules if you don’t want to required an email (you will also need to remove the index in mysql: uniq_email – BTREE)
Copy modules/orm/classes/model/auth/user.php to application/classes/model/auth/user.php and change the public function rules() to your needs

It’s a good idea to add a new role for your normal user, that way you can list them easily:
[ccnw_php width=”550″ theme=”mac-classic”]
$p = ORM::factory(‘role’, array(‘name’ => ‘participant’))->users->find_all();
[/ccnw_php]

Create application/messages/models/user.php
[ccnw_php width=”550″ theme=”mac-classic”]
‘Username must be unique’,
’email.unique’ => ‘Email must be unique’,
);
[/ccnw_php]

If you want to use the remember feature:
[ccnw_php width=”550″ theme=”mac-classic”]
$remember = isset($post[‘remember’]);
Auth::instance()->login($post[‘username’], $post[‘password’], $remember)
[/ccnw_php]

then you need to be sure to have a cookie salt in application/bootstrap.php
[ccnw_php width=”550″ theme=”mac-classic”]
/**
* Cookie salt for remember user info
*/
Cookie::$salt = ‘fdsh-tretgd-re-gfds-gt-erg-fdg-‘;
[/ccnw_php]

Some useful stuff:
[ccnw_php width=”550″ theme=”mac-classic”]
// Add an administrator (in a temporary controller)
$model = ORM::factory(‘user’);
$model->values(array(
‘username’ => ‘admin’,
‘password’ => ‘admin’,
‘password_confirm’ => ‘admin’,
’email’ => ‘your@email.com’,
));
$model->save();
// remember to add the login role AND the admin role
// add a role; add() executes the query immediately
$model->add(‘roles’, ORM::factory(‘role’)->where(‘name’, ‘=’, ‘login’)->find());
$model->add(‘roles’, ORM::factory(‘role’)->where(‘name’, ‘=’, ‘admin’)->find());
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// If this user doesn’t have the admin role, and is not trying to login, redirect to login
public function before()
{
parent::before();
if ( ! Auth::instance()->logged_in(‘admin’) AND Request::current()->uri() !== ‘manage’)
{
$this->request->redirect(‘/manage’);
}
}
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// Administrator already logged in, redirect to dashboard
if (Auth::instance()->logged_in(‘admin’)) {
$this->request->redirect(‘manage/dashboard’);
}
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// Log the user
Auth::instance()->login($post[‘username’], $post[‘password’], FALSE)
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// Check if the user have the admin permission
if(!Auth::instance()->logged_in(‘admin’)) {
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// Log user out
Auth::instance()->logout();
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// check if email or username (automagic) is already taken
if(ORM::factory(‘user’)->unique_key_exists($_POST[‘username’])) {
echo “FOUND”;
}
[/ccnw_php]

 
ORM
http://kohanaframework.org/3.2/guide/orm/
http://karlsheen.com/kohana/kohana-3-orm-tutorial-and-samples/
http://kohanaframework.blogspot.com/2010/12/kohana-3-orm-simple-example.html
http://www.geekgumbo.com/2011/05/24/kohana-3-orm-a-working-example/
http://kohanaframework.org/3.2/guide/api/ORM

ORM is included with the Kohana 3.x install but needs to be enabled before you can use it. In your application/bootstrap.php file modify the call to Kohana::modules and include the ORM modules:
[ccnw_php width=”550″ theme=”mac-classic”]
‘orm’ => MODPATH.’orm’,
[/ccnw_php]

The table name must be in plural;
The table must have an id with auto increment (required);
You must create a Model that extends ORM class (this one not in plural)

1)
Create a table with a “s” as the end:

tracking -> trackings
category -> categories

2)
Create a model (application/classes/model) without the “s”

tracking.php
category.php

[ccnw_php width=”550″ theme=”mac-classic”]
3)
Establish your relation (one-to-one, one-to-many etc…)
http://kohanaframework.org/3.2/guide/orm/relationships

4)
Use your model / ORM
[ccnw_php width=”550″ theme=”mac-classic”]
try
{
$tracking = ORM::factory(‘tracking’);
$tracking->user_id = Auth::instance()->get_user()->id;
$tracking->session_id = Session::instance()->id();
$tracking->title = ‘ fdsfdsf ‘;
if($tracking->save()) {
echo “save”;
} else {
echo “for some reason, there’s an error”;
}
}
catch (ORM_Validation_Exception $e) {
echo “error”;
var_dump($e->errors());
}
[/ccnw_php]

Some useful stuff:
[ccnw_php width=”550″ theme=”mac-classic”]
$user = ORM::factory(‘user’);
echo $user->count_all();
echo $user->last_query();
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
$playlists = ORM::factory(‘playlist’)->where(‘week_id’, ‘=’, 1)->find_all();
foreach ($playlists as $playlist) {
echo $playlist->url;
}
[/ccnw_php]

Last ID from ->save();
[ccnw_php width=”550″ theme=”mac-classic”]
$myormmodel->save();
echo $myormmodel->id();
[/ccnw_php]

Dynamic ORM query builder
[ccnw_php width=”550″ theme=”mac-classic”]
public function search_keywords(array $keywords)
{
foreach($keywords as $keyword)
{
$this->or_where(‘title’, ‘like’, ‘%’.$keyword.’%’);
}
return $this->find_all();
}
[/ccnw_php]


ORM Validation
http://kohanaframework.org/3.2/guide/orm/examples/validation
http://kohanaframework.org/3.2/guide/kohana/security/validation
[ccnw_php width=”550″ theme=”mac-classic”]
array(
array(‘not_empty’),
array(‘min_length’, array(‘:value’, 4)),
array(‘max_length’, array(‘:value’, 32)),
array(array($this, ‘username_available’)),
),
‘password’ => array(
array(‘not_empty’),
),
);
}
public function filters()
{
return array(
‘password’ => array(
array(array($this, ‘hash_password’)),
),
);
}
public function username_available($username)
{
// There are simpler ways to do this, but I will use ORM for the sake of the example
return ORM::factory(‘member’, array(‘username’ => $username))->loaded();
}

public function hash_password($password)
{
// Do something to hash the password
}
}
[/ccnw_php]

 
Database:
http://kohanaframework.org/3.2/guide/database/
http://kohanaframework.org/3.2/guide/api/Database

There’s 2 ways to query a database: prepared (normal SQL) and query builder (dynamic)

Prepared:
[ccnw_php width=”550″ theme=”mac-classic”]
$query = DB::query(Database::SELECT, ‘SELECT * FROM users WHERE username = :user AND status = :status’);
 
$query->parameters(array(
    ‘:user’ => ‘john’,
    ‘:status’ => ‘active’,
));
[/ccnw_php]

Query builder:
[ccnw_php width=”550″ theme=”mac-classic”]
$query = DB::select()->from(‘users’)->where(‘username’, ‘=’, ‘john’);
$query = DB::select(‘username’)->distinct(TRUE)->from(‘posts’);
$query = DB::select()->from(`posts`)->limit(10)->offset(30);

[/ccnw_php]
Results:
[ccnw_php width=”550″ theme=”mac-classic”]
$results = DB::select()->from(‘users’)->where(‘verified’, ‘=’, 0)->execute();
foreach($results as $user)
{
    // Send reminder email to $user[’email’]
    echo $user[’email’].” needs to verify his/her account\n”;
}
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
$results = DB::select()->from(‘users’)->where(‘verified’, ‘=’, 0)->as_object()->execute();
foreach($results as $user)
{
    // Send reminder email to $user->email
    echo $user->email.” needs to verify his/her account\n”;
}
[/ccnw_php]
Only get 1 result:
[ccnw_php width=”550″ theme=”mac-classic”]
$total_users = DB::select(array(‘COUNT(“username”)’, ‘total_users’))->from(‘users’)->execute()->get(‘total_users’, 0);
[/ccnw_php]
[ccnw_php width=”550″ theme=”mac-classic”]
// Get the total number of records in the “users” table
$db = Database::instance();
$count = $db->count_records(‘testi’);
[/ccnw_php]

 
3rd party libraries
The convention is to place 3rd party files in application/vendor. For instance, if you had an installation of Doctrine, you would place it in application/vendor/doctrine.
[ccnw_php width=”550″ theme=”mac-classic”]
require Kohana::find_file(‘vendor’, ‘Swift-4.0.5/lib/swift_required’);
$transport = Swift_SmtpTransport::newInstance(…); // This is autoloaded for me by Swiftmailer
[/ccnw_php]

PHPExcel – create PDF, CSV, Excel:
application/vendor/phpexcel/PHPExcel.php & PHPExcel
then in your controller:
[ccnw_php width=”550″ theme=”mac-classic”]
require Kohana::find_file(‘vendor’, ‘phpexcel/PHPExcel’);
$objPHPExcel = new PHPExcel();
[/ccnw_php]

Email – you can use this module:
https://github.com/Luwe/Kohana-Email
or directly use swiftmailer:
http://swiftmailer.org/

[ccnw_php width=”550″ theme=”mac-classic”]
require Kohana::find_file(‘vendor’, ‘swift/swift_required’);
//Create the Transport
$transport = Swift_SmtpTransport::newInstance(‘localhost’, 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance(‘Email’)
->setFrom(array(‘from@email.net’ => ‘From’))
->setTo(array(‘to@email.net’))
->setBody(‘An email’);
//Send the message
$result = $mailer->send($message);
[/ccnw_php]

 
Helper
http://kohanaframework.org/3.2/guide/api/Arr
If you want to add some custom helper (generally used statically) or library (instantiated / object), for example: application/classes/participants.php
[ccnw_php width=”550″ theme=”mac-classic”]
 
Tips
To get the params in a controller (depending on your routes in application/bootstrap.php)
[ccnw_php width=”550″ theme=”mac-classic”]
$this->request->param(‘id’)
[/ccnw_php]
Debug:
[ccnw_php width=”550″ theme=”mac-classic”]
echo Debug::vars();
[/ccnw_php]
To get the current controller:
[ccnw_php width=”550″ theme=”mac-classic”]
echo Request::current()->uri();
[/ccnw_php]
To point at the right directory use:
[ccnw_php width=”550″ theme=”mac-classic”]

[/ccnw_php]
To get a custom column from users (auth):
[ccnw_php width=”550″ theme=”mac-classic”]
echo Auth::instance()->get_user()->week;
[/ccnw_php]
Date:
[ccnw_php width=”550″ theme=”mac-classic”]
Date::formatted_time(‘now’, ‘m-d-Y’);
//constant Date::WEEK Date::YEAR
[/ccnw_php]

Way to use the model and the post for edition in form:
[ccnw_php width=”550″ theme=”mac-classic”]
// Received the POST
if (isset($_POST) && Valid::not_empty($_POST)) {
// keep but not saved
$participant->values($_POST, array(’email’, ‘username’,’password’))
}
[/ccnw_php]
To redirect use:
[ccnw_php width=”550″ theme=”mac-classic”]
$this->request->redirect(‘manage/dashboard’);
[/ccnw_php]
To make a link use:
[ccnw_php width=”550″ theme=”mac-classic”]
‘color: #FFF’)); ?>
[/ccnw_php]

Function inside controller:
inside action_x():
[ccnw_php width=”550″ theme=”mac-classic”]
array_walk($trackings, array($this, ‘_replaceplaylistendtime’), $params);
[/ccnw_php]
outside action_x():
[ccnw_php width=”550″ theme=”mac-classic”]
static function _replaceplaylistendtime(&$value, $key, $p) {

}
[/ccnw_php]

 
Jquery
Use the latest minified version on google server:
http://code.jquery.com/jquery.min.js

User interface:
http://ninjaui.com/
http://jqueryui.com/
http://flowplayer.org/tools/index.html

Ajax:
In your “ajax” controller / action
[ccnw_php width=”550″ theme=”mac-classic”]
if ($this->request->is_ajax()) {
$id = json_decode($_POST[‘refresh’]);
$this->auto_render = FALSE;
echo json_encode(array(‘result’ => $id));
}
[/ccnw_php]
In your jquery:
[ccnw_php width=”550″ theme=”mac-classic”]
$.ajax({
type: “POST”,
url: “home/refresh”,
data: “refresh=1”,
async: true,
dataType: “json”,
success: function(resultArray, textStatus, XMLHttpRequest)
{
var result = parseInt(resultArray[‘result’]);
alert(result);
},
error: function(request, textStatus, errorThrown)
{
alert(‘error refreshing the session’);
}
});
[/ccnw_php]

 
Bookmarks
http://kohanaframework.org/3.2/guide/kohana/tutorials/
http://kohanaframework.org/3.2/guide/api or http://kohana.nerdblog.pl/api/

 
Deploying:
See this for more information about deploying Kohana application
http://nerdblog.pl/2011/09/05/deploying-kohana-3-2-application-in-production/

04

04 2010

0 Comments Add Yours ↓

  1. psc #
    1

    hi Omry,

    have a look at my site template that use authentication & internationalization:
    https://github.com/patricksebastien/kohana-3.2-example
    https://www.workinprogress.ca/ko32example/site/

    regards

  2. 2

    hi,
    i would like to see i18n implemenatation, for content, forms and messages.

    regards,
    Omry

  3. psc #
    3

    Hi Sinan,

    GIT doesn’t add empty directories. Fixed (by adding a .gitignore).

  4. Sinan E. #
    4

    Please add “cache” and “logs” folders to your application folder.
    In unix system it causes errors if they do not exist.
    Thanks for the project.

  5. psc #
    5

    Hi Robert,

    You’re right, I forgot to add the “reset_token” field in “users”. You have to implement the mailing functionality yourself (you can see an example in this post under 3rd party libraries). After that, the reset password will work. About the logging, the “Remember” checkbox should add a cookie BUT something is missing in “config/ko32example.php”: session_lifetime => 0. Will fix it soon.

    The best thing would be to make a pull request on github if you want to contribute your code.

    EDIT: fixed on github

  6. Robert #
    6

    Thanks for the excellent article/code example. It really helps me a lot to implement my own authentication stuff.

    I have a few questions, and I hope you are willing to answer them.

    In function action_password() in class Controller_Account you have got the following line:

    $user = ORM::factory(‘user’)->where(’email’, ‘=’, $_GET[’email’])->where(‘reset_token’, ‘=’, $_GET[‘token’])->find();

    If I am correct, there is no field “reset_token” in table users.

    So action_reset does not work, or does it?

    I noticed you commented out the mailing stuff and replaced it with “//TODO email”.

    That is no problem, because I think I will be able to add the mailing functioality myself.

    A few questions though:

    – Did you implement it afterwards? If so, is the code available?

    – Do you use a field “reset_token”, or do you use table “user_tokens” to store the tokens? And if you haven’t implemented this part (yet), what would you advise to do?

    It would be best fantastic if you could complete action_reset and action_password.

    I am going to try it myself, but I am fairly new to Kohana, so it won’t be easy. If I succeed, I will send you a copy.

    Thanks again for the article/code example.

    Cheers,
    Robert

    PS: It looks like the user stays logged in. It does not make a difference you choose ‘remember’ or not. A small bug?

  7. 7

    Thanks for this article! I’m sure many will find this very useful. Also thanks for showing me http://kohana.nerdblog.pl/api/ :)



Your Comment