Plugins "Kohana-for-Wordpress" and "Wordpress-for-Kohana" at a glanceСреда, 7 октября 2009 г.
Рубрика: Web frameworks
Метки: Kohana | обучение | плагин
Подписаться на комментарии по RSS
Это перевод краткого обзора "Плагины Kohana-for-Wordpress и Wordpress-for-Kohana – быстрый взгляд"
Plugins branch on the official Kohana forum
This Kohana 3 plugin allows Kohana 3 to be integrated into WordPress system. Integration means the possibility to call Kohana3 controllers from WP entities like templates, widgets, posts/pages, and the possibility to call controllers via URI requests.
To install plugin you should follow instructions included in the distributive (I tested stuff on Wordpress 2.8.4, XAMPP, Windows, PHP 5.2.8):
1. Install Kohana3. Simply copy application/, modules/, system/ folders where you need (for me – in DocumentRoot)
2. Add unpacked plugin in wp-content/plugins/ folder
3. Login into WP admin area and activate plugin
4. Go to plugin config page and see two tabs
Kohana Options tab:
What is Kohana Front Loader — see below.
- Kohana Application Path — full absolute path to application/ folder (I have D:\xampp\htdocs\wp284.loc\www\application\)
- Kohana Module Path — full absolute path to modules/ folder (I have D:\xampp\htdocs\wp284.loc\www\modules\)
- Kohana System Path — full absolute path to system/ folder (I have D:\xampp\htdocs\wp284.loc\www\system\)
- Custom Bootstrap Path — if you want to use your own bootstrap instead of given kohana_bootstrap.php, set its path here (you should use kohana_bootstrap.php as an example, because it has necessary modifications — plugin options are set by native WP-functions and no echo Request::instance()->execute()->send_headers()->response).
- Kohana File Extension — you know what it is
- Default Placement — defines what should plugin do with Kohana controller response on URI requesr: put it (responce) before ordinary page content (Before…), after (After…) or replace content (Replace…).
- Kohana Modules — comma separated used Kohana 3 modules
- Kohana Default Controller / Action / ID — default controller and method are required (otherwise plugin will not work). ID is optional.
- Include Kohana Front Loader in Wordpress Navigation — switch on if you want to see Front Loader in the blog navigation
- Process all URIs — when switched off, plugin will process URI requests only from Kohana Loader Page (for me it is like http://wp284.loc/kohana?kr=controller/method/ID); if switched on, plugin will inspect queries from all pages whether they are Kohanian (see below)
Next tab — Page Routing
We have to deal with it when want to “automatically” include controller response in existing WP page (without any manual writing on the page or template)
- Kohana Controller — write controller/method[/id]
- Wordpress Page — choose the WP page (static only) to include in
- Placement — where Ko3 response will be placed on the page: before content (Before…), after (After…) or even replace content (Replace…)
Add route and after it appears in the table, you can add other route (I have not experimented nor examined code what happen if some routes have common WP page, it’s interesting . Route from table can only be deleted, not edited. One more little unhandy feature – only ID of attached WP page showed, I’d like to see something more human-readable, like page name or slug.
How to work with controller invocation from WP entities
- from WordPress template:
to invoke 'controller/action/parameter'. Result is that you assigned in called method’s $this->request->response.
You can call Views and really do almost all that you can do in native Kohana 3 (some session problems were reported but maybe they have already been fixed).
- from WordPress widget:
Drag Kohana widget (it is created automatically at plugin activation) on sidebar, set widget header and needed 'controller/action/parameter'. That’s all.
- from static page or blog post:
In WP page editor add
Nice and simple.
Kohana-for-Wordpress plugin adds filter to WP URI request processing, and every request some magic happens (yes, its logic was a little bit tricky for me :
- if requested WP page does not exist and “Process all URIs” option switched on, plugin checks whether existing Kohana controller was requested: first it looks in GET for variable 'kr' containing 'controller/action/parameter' (e.g. http://wp284.loc/index.php?kr= welcome/index/1). If nothing found, plugin parses SERVER['REQUEST_URI'] to find some request in a convenient Kohana “controller/method/parameter” format (yes, you can write like http://wp284.loc/welcome/index and it will work – if “welcome/index” were defined).
Note, that controller response will be rendered on the Front Loader page. So if you have not added the page to the Page Routing, response will appear on the Front Loader.
- if requested WP page is the Kohana Front Loader page, the request will be parsed like above. When the query string is empty, plugin will take default controller/action[/parameter] from configuration.
- otherwise plugin checks if requested WP-page is included in Page Routing. If yes, a defined query is processed and output goes in a predefined place.
Query details are added to the global $wp->kohana object containing plugin data
- After some options update (e.g. “Process all URIs”, “Include Kohana Front Loader in Wordpress Navigation” etc) all slashes in Kohana folders paths (on the plugin options page) were removed and I should to insert them manually. Maybe it is some environment feature but it happens.
- When “Process all URIs” option switched on, some admin pages (e.g. /wp-admin/edit-pages.php, /wp-admin/edit.php, /wp-admin/upload.php) try to be processed by Kohana, but no ‘wp-admin’ controller can be found and Kohana throws exception.
What is Wordpress-for-Kohana? It is Kohana 3 plugin for Kohana-for-Wordpress , that offers WP-style views for Ko3, and some WP-style Ko3 methods. Copy plugin folders in application/ folder of Kohana(-on-WordPress) installed, and you can do something like that:
In WP page template (page.php) replace get_footer() by kohana('wordpress/footer’) – and Kohana view ‘footer’ from views/wp/ folder become WP footer. Enjoy!
Want more templates? Add methods to Controller_Wordpress (stuff there is self-explaining), add templates in views/wp/.
In Wordpress-for-Kohana templates you can use static methods of the Kohanian Wordpress class, named apparently: slug(), get_id_from_name(), get_post_by_name(), recent_posts(), postid().
Kohana-for-Wordpress and Wordpress-for Kohana plugins have made good impression although I did not work with WP last time. It was interesting to see an example of Kohana integration in the mature CMS. I think that after some similar plugins appear (e.g. for Joomla and Drupal) Kohana will become a very popular framework.