Table of contents


DISCLAIMER: most materials is get from web, I just resort it for study purpose, you can find the source where this article from in resource section.

Basic Rules and Concepts

  1. Fat models, skinny controllers!
  2. Keep as much business logic in the model as possible.
  3. If you see your controller getting “fat”, consider offloading some of the logic to the relevant model (or else bad things will start happening!).
  4. Models should not talk to the views directly (and vice versa).
  5. Related models provide information to the controller via their association (relation).
  6. It’s quite alright for the views to contain some logic, which deals with the view or presentation.

Debugging

Name Convention

Class                       File

Controller_Template         classes/controller/template.php
Model_User                  classes/model/user.php
Model_Auth_User             classes/model/auth/user.php
Auth                        classes/auth.php

Configuration

  • set $config['display_errors'] = TRUE; to enable error messages showing in page. It can also check error messages in log file. Check your settings in config/log.php to be sure.
  • set your $config['threshold'] = 1;. This sets log threshold to a suitable level for production. Higher threshold levels will log less critical notices and information, but can slow down your application.

Kohana::debug()

Profiler

http://docs.kohanaphp.com/libraries/profiler

Log File

SQL Query Logging

http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/

Page Flow

Every application follows the same flow:

  1. Application starts from index.php
  2. Includes APPPATH/bootstrap.php
  3. Request::instance called to process the request
    1. Checks each route until a match is found
    2. Loads controller and passes the request to it
    3. Calls the Controller::before method
    4. Calls the controller action
    5. Calls the Controller::after method
  4. Displays the Request response

Sequence Diagram.(TBD)

Enable Module

    /**
     * Enable modules. Modules are referenced by a relative or absolute path.
     */
    Kohana::modules(array(
        // 'auth'       => MODPATH.'auth',       // Basic authentication
        // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
        // 'database'   => MODPATH.'database',   // Database access
        // 'image'      => MODPATH.'image',      // Image manipulation
        // 'orm'        => MODPATH.'orm',        // Object Relationship Mapping
        // 'pagination' => MODPATH.'pagination', // Paging of results
        // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
        ));

URI

kohana101_01.png

  • http://localhost/kohana : No segments, so a default controller is used.
  • http://localhost/kohana/index.php : Still no segments, so same as before.
  • http://localhost/kohana/index.php/welcome : Only one segment, which is segment 1. This corresponds to a controller called “Welcome”
  • http://localhost/kohana/index.php/welcome/search/php : Three segments given:
    1. Segment 1 is “welcome” and corresponds to controller “Welcome”
    2. Segment 2 is “search” and corresponds to method “search()” in controller “Welcome”
    3. Segment 3 is “php” and corresponds to argument 1 passed to method “search()” in controller “Welcome”

System Components

The most important Kohana are Controllers, Libraries, Helpers, Modules, Models and Views

  • Models: Separate the data access from the logic and presentation.
  • Views: Simplifies the organization of information for display
  • Controllers: Are the heart of your application, implementing it’s logic.
  • Libraries: Provide the functionality that drives your application; Database access, Sessions, Caching 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.

System Layout

There are three layers to Kohana; Application, Module, System. A very important concept to understand in Kohana is the cascade. The cascade refers to where and how Kohana searches for resources. A specific naming convention and directory layout is enforced. A specific search order is enforced.

------- Search order direction ------->
(1)             --> (2)             --> (3)
application/    -->  modules/       -->  system/
    controllers/-->     controllers/-->     controllers/
    helpers/    -->     helpers/    -->     helpers/
    libraries/  -->     libraries/  -->     libraries/
    models/     -->     models/     -->     models/
    views/      -->     views/      -->     views/

kohana101_02.png

Database

Copy system/config/database.php to application/config/database.php

    $config['default'] = array
    (
        'benchmark'     => TRUE,
        'persistent'    => FALSE,
        'connection'    => array
        (
            'type'      => 'mysql',
            'user'      => 'username',       // set to db user name
            'pass'      => 'password',       // set to db user password
            'host'      => 'localhost',
            'port'      => FALSE,
            'socket'    => FALSE,
            'database'  => 'db name'     // set to db name
        ),
        'character_set' => 'utf8',
        'table_prefix'  => '',
        'object'        => TRUE,
        'cache'         => FALSE,
        'escape'        => TRUE
    );

Five ways to work with relational databases

  1. Write your own SQL using 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. It provides methods that generate platform independent SQL statements that are automatically escaped.
  4. Use a Model. Utilize Query or Query Builder to create your own data access methods.
  5. Use ORM. Object Relational Mapping is a class that extends the basic Model, to implement data access without using SQL statements.

Constants

  • DOCROOT - docroot real path
  • KOHANA - front controller name (index.php)
  • APPPATH - application folder real path
  • MODPATH - module real folder path
  • SYSPATH - system real folder path

KO3 struct

kohana
  |-
  |-application
    |-cache
    |-config
    |-classess
      |-controller
      |-model
      |-other_libraries.php
    |-i18n
    |-logs
    |-messages
    |-vendor
    |-views
    |-bootstrap.php
  |-modules
  |-system
  index.php

Resource