using kohana

Installation is really simple.

Download Kohana:

PHP compilation for Kohana:

./configure --with-apxs2=/opt/apache/bin/apxs --prefix=/opt/php --with-config-file-path=/opt/php --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-mbstring=all --enable-sockets --enable-sysvsem --enable-sysvshm --enable-mbregex --with-tidy --with-openssl --with-zlib-dir --with-zlib --with-bz2 --with-jpeg-dir --with-curl --with-gd --with-png-dir --with-ttf --with-mime-magic --with-gettext --with-gmp --with-imap --with-kerberos --with-imap-ssl --with-mcrypt --with-mhash --with-pspell --with-regex=php --with-xmlrpc --with-iconv --enable-gd-jis-conv --with-pgsql=/opt/postgres --with-pdo-pgsql=/opt/postgres --with-ttf --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2

Extract it and put only index.php and install.php in the htdocs folder. Place system, modules, application in ../. Edit index.php to reflect the path of those directories:

$kohana_application = '../application';
$kohana_modules = '../modules';
$kohana_system = '../system';

Set the permission:

chmod 666 application/logs

That's it. Point your browser to index.php. You should see Welcome to Kohana! Now time to read documentation… Here's a PDF that might help you:

Here's what you need to remember if you are doing MVC:
• The View is about what the user sees.
• The Controller is about what the user does.
• The Model is about what the user gets.

Kohana application must have a configuration file called application/config/config.php It may not be renamed or relocated.

Read this slowly
When PHP processes index.php the Kohana framework is invoked, system files are loaded, and the URI is inspected to determine what actions need to be taken. These actions correspond to Controllers and their associated methods. If the controller is invoked without a method segment, this function (index()) is called. A View file consists of the information you want to output to a browser.

• Controllers : Are the heart of your application, implementing it's logic.
• Libraries : Provide the functionality that drives your application; Database access, Sessions, Cacheing etc.
• Helpers : Assist you with common tasks; Formatting HTML, Sending email, Validating, Creating HTML forms etc.
• Modules : Assist in grouping common components or functionality: Authentication, Handling media etc.
• Models : Separate the data access from the logic and presentation.
• Views : Simplifies the organisation of information for display.

Kohana implements a namespace, so controllers, models and views can have the same name.
Kohana convention is to use under_score naming.

Class name: Name_Controller extends Controller (First letters uppercase)
File name: controllers/controller.php (lowercase)

Class name: name_Core (First letter lowercase)
File name: helpers/helper.php (lowercase)

Class name: Name_Core (First letters uppercase)
File name: libraries/Library.php (First letter uppercase)

Class name: Name_Model extends Model(First letters uppercase)
File name: models/model.php (lowercase)

Class name: It's not a class.
File name: views/view.php (lowercase)

Controllers are not instantiated manually, the system does that, you only extend the class.

Helpers have static methods, they do not have to be instantiated, just call the class method. Example:
url::redirect(''); will emit a location header.
You can use a helper anywhere, controller, model, view.

Models must be instantiated. The object must be assigned. $this->model = new
Model_Class_Name('model_file_name') A model should be instantiated in the controller. Note:
A model does not have access to library objects instantiated by the controller, you need to instantiate a new
object, or use Kohana::instance()->library->method()

Libraries must be instantiated. The object must be assigned. $this->library = new
Library_Class_Name('Library_file_name') A library should be instantiated in the controller.

Some system methods are static. This allows for convenient access to the system, you do not need to
instantiate anything to use them. Examples are Kohana::config('item') which returns a config entry.
Kohana::debug($variable) which pretty prints any variable for debugging.

Changing the default controller (welcome)?
We must tell Kohana that the default controller is no longer called "Welcome" Copy system/config/routes.php to application/config/routes.php and edit the file, changing the defined default route, as below:

$config['_default'] = 'home';


Copy system/config/database.php to application/config/database.php and edit it.

Kohana provides five ways to work with relational databases:
1. Write your own SQL. Use the query() method. You must escape the query manually.
2. Write custom SQL, that is automatically escaped. Use Query Binding.
3. Use the Query Builder. Provides methods that generate platform independant SQL statements that
are automatically escaped.
4. Use a Model. Utilise Query or Query Builder to create your own data access methods.
5. Use Object Relational Mapping. ORM is a class that extends the basic Model, to implement data
access without using SQL statements.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License