This is documentation for Kohana v2.3.x.
Status | First Draft |
---|---|
Todo | Requires more development |
Users of Kohana version 1.x (“Blueflame”) or CodeIgniter 1.x migrating to Kohana 2.0 can follow these steps to migrate their application.
Starting with a fresh Kohana install, delete application
folder, and copy your existing application folder to the same location.
Remove your old config
folder.
application/config
directory from Kohana 2.x to application/config
application/config/config.php
, the main configuration files for your applicationThe logs directory needs to be writable by the webserver or you can turn logging off.
Rename all your controllers to {NAME}_Controller
. For example, if your old controller was Page
, make it Page_Controller
.
Make your Controller contructors PHP5 if needed:
function __construct()
instead of function Page()
parent::__construct()
instead of parent::Controller()
Rename all your models to {NAME}_Model
PageModel
, make it Page_Model
$this→load→model('page')
__construct()
function, be sure to call parent::__construct()
If you have a base controller for your application (in the libraries folder) you will need to:
MY_Controller extends Controller
to Controller extends Controller_Core
MY_Controller
in your controllers to Controller
The CI function uri_to_assoc($offset)
becomes segment_array($offset,$associative)
with $associative
set to TRUE
.
Class names need to have _Core
appended to them and be capitalized. The file names should also have the same caps as the class name (without the core
).
References to those classes need to be capitalized to match the library calls (without the core
).
$this→load→
is deprecated. Kohana uses auto loading so you can instantiate an object (e.g. new View()) without including the class first.
Change all your helper calls to the new syntax: helper::function()
html::anchor()
instead of anchor()
url::base()
instead of base_url()
form::open()
instead of form_open()
system/helpers
If you have custom helpers they need to be changed. Assuming your helper file is foo.php
:
class foo { }
public static
in front of all the function names
Calls are now made via foo::function()
.
Note also that the CodeIgniter helpers and libraries typically have this line at the top of the script:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
This is not valid for Kohana so if you have copied this line for your own helpers etc you need to change it to the following to work in Kohana:
<?php defined('SYSPATH') or die('No direct access allowed.');
Views are now treated somewhat differently. Instead of being “flat” files, views are now objects. This allows much greater flexibility, and easier “view-in-view” handling.
// Load the view and set the $title variable $view = new View('template', array('title' => 'User Details')); // Sets the $username variable in the view $view->username = 'JohnDoe'; // Sets the $visits variable to another view $view->visits = new View('user/visits', array('user_id' => 3)); // Renders the view to a string $str_view = $view->render(); // Displays the view $view->render(TRUE);
Note: Using print
or echo
on a View will cause it to render into a string. This is very useful in Views:
<!-- This example is the "template" view from above --> <h1><?php echo $title ?> for <?php echo $username ?></h1> <div id="visits"><?php echo $visits ?></div>
In the above example, a View object, $visits
, was used as a string. This syntax is encouraged because it is very short and readable when mixed with HTML.
There is a important note, in CI you can use the $this in the model and you have the same libraries as your controller, in kohana only the db library is loaded on a model. If you need more libraries you have two options:
// Create a new object with the library $uri = new Uri; $value = $uri->segment(3); // Can not use $this->uri->segment(3) as used in CI // Use the instance of your controller $value = Kohana::instance()->uri->segment(3);