Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kohana Framework

Kohana Framework

Kohana is an elegant HMVC PHP5 framework that provides a rich set of components for building web applications. These slides describe some useful features in Kohana.

Leong Hean Hong

November 28, 2012
Tweet

More Decks by Leong Hean Hong

Other Decks in Programming

Transcript

  1. Origin • Fork from CodeIgniter • v2.x = CI +

    ORM on PHP5 • v3.x = Complete rewrite of v2.x
  2. Code Organization /vendor/ - 3rd party libraries. /system/ - System

    core. /modules/ - Sub MVC modules. E.g. Auth, ORM, Database, Unit Test, ... /application/classes/ - All classes, including controllers & models. /application/config/ /application/views/ /application/i18n/ - Translations
  3. Convention • Underscore instead of underscore/camelcase mixture. • Underscores in

    class name reflects location. Controller_Welcome -> classes/Controller/Welcome.php Database_Query -> classes/Database/Query.php • BSD/Allman style bracketing
  4. Namespace • Have not utilized PHP namespacing. • Depends on

    underscore naming convention to avoid conflicts. (not ideal)
  5. Cascading Filesystem • EVERY (system) classes can be extended/replaced. •

    /application/classes/Encrypt.php replaces /system/classes/Encrypt.php • /application/config/auth.php replaces /modules/auth/config/auth.php
  6. Autoloading • The cascading filesystem defines how Kohana search for

    files. Kohana: $foo = new Foo; CakePHP: App::uses('Lib', 'Foo'); $foo = new Foo();
  7. Model • Non-ORM by default. • Model can extend ORM

    class. • Supports query builder. • Query returns model objects, instead of array • Associated models loads on demand.
  8. Model $user = ORM::factory('User') ->where('id', '=', 20) ->find(); $posts =

    $user->Posts->find_all(); foreach ($posts as $post) { echo $post->publish_date; }
  9. View • Normal PHP, no templating engine. • Supports views

    in views echo View::factory('user/login')->bind('user', $user); Loading view in controller: public function action_about() { $this->response->body(View::factory('pages/about')); }
  10. Routing • Supports regex Route::set('default', '(<controller>(/<action>(/<id>)))') ->defaults(array( 'controller' => 'Welcome',

    'action' => 'index', )); Route::set('gallery', '<action>(<controller>):<id>', array( 'controller' => '[A-Z][a-z]++', 'action' => '[A-Z][a-z]++', )) ->defaults(array( 'controller' => 'Slideshow', ));
  11. Routing • Custom function Route::set('testing', 'foo') ->filter(array('Class', 'method_to_process_my_uri')); Route::set('save-form', 'save')

    ->filter(function($route, $params, $request) { if ($request->method() !== HTTP_Request::POST) { return FALSE; // This route only matches POST requests } });
  12. Routing • Name based routing Route::set('feeds', '<user_id>(/<action>).<format>', array( 'user_id' =>

    '\d+', 'format' => '(rss|atom|json)', )) ->defaults(array( 'controller' => 'Feeds', 'action' => 'status', )); $url = Route::get('feeds')->uri(array( 'user_id' => $user_id, 'action' => 'comments', 'format' => 'rss' ));
  13. Support • Active and helpful forum. • Get answers at

    StackOverflow • Lack documentation. • Source code well documented, easy to understand. • API browser generated using PHPDoc.
  14. Automagic Overload Kohana: echo Form::input('name', 'home'); <input type="text" name="name" value="hong"

    /> CakePHP: <?php echo $this->Form->input('name'); ?> <div class="input text"><label for="UserName" >Name</label><input name="data[User][name]" type="text" id="UserName"/></div> <?php echo $this->Form->input('name', array('div' => false, 'label' => false)); ?> <input name="data[User][name]" type="text" id="UserName"/>