Table of contents
- Basic Rules and Concepts
- Name Convention
- Page Flow
- Enable Module
- System Components
- System Layout
- KO3 struct
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
- Fat models, skinny controllers!
- Keep as much business logic in the model as possible.
- If you see your controller getting “fat”, consider offloading some of the logic to the relevant model (or else bad things will start happening!).
- Models should not talk to the views directly (and vice versa).
- Related models provide information to the controller via their association (relation).
- It’s quite alright for the views to contain some logic, which deals with the view or presentation.
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
$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.phpto 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.
SQL Query Logging
Every application follows the same flow:
- Application starts from index.php
- Includes APPPATH/bootstrap.php
- Request::instance called to process the request
- Checks each route until a match is found
- Loads controller and passes the request to it
- Calls the Controller::before method
- Calls the controller action
- Calls the Controller::after method
- Displays the Request response
/** * 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 ));
- 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
- Segment 1 is “welcome” and corresponds to controller “Welcome”
- Segment 2 is “search” and corresponds to method “search()” in controller “Welcome”
- Segment 3 is “php” and corresponds to argument 1 passed to method “search()” in controller “Welcome”
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.
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/
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
- Write your own SQL using the query() method. You must escape the query manually.
- Write custom SQL that is automatically escaped. Use Query Binding.
- Use the Query Builder. It provides methods that generate platform independent SQL statements that are automatically escaped.
- Use a Model. Utilize Query or Query Builder to create your own data access methods.
- Use ORM. Object Relational Mapping is a class that extends the basic Model, to implement data access without using SQL statements.
- 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
kohana |- |-application |-cache |-config |-classess |-controller |-model |-other_libraries.php |-i18n |-logs |-messages |-vendor |-views |-bootstrap.php |-modules |-system index.php