Kohana 3.0の紹介-パート5

Kohana PHP V3(KO3)を使用した開発記事シリーズの第5部をご覧ください。 前のパーツは、「 kohana 3.0に慣れている 」というラベルの下にあります。 このパートでは、HMVC(Hierarchical-Model-View-Controller)を見ていきます。



HMVCはMVC(model-view-controller)の続きであり、コントローラが他のコントローラに階層的な順序で特定の指示を与えることができます。 これを理解するために、単独でまたはグループで動作できるモジュールを想像してください。 それでもうまくいかない場合は、このHierarchical-Model-View-Controllerパターンの図をご覧ください。



以前は、複数のアクションを持つ単一のコントローラーがモデルからデータを取得し、ビューを1つの本格的なWebページに適切に構成するページを設計しました。 HMVCを使用すると、コードをより適切に編成し、再利用の準備を整えることができます。 また、HMVCがKohana 3に実装されている方法を使用すると、セクションのコンテンツのために他のサーバーにアクセスすることもできます。



新しいコントローラーを作成しましょう:



<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Hmvc extends Controller_DefaultTemplate { public function action_index() { // Set meta data $this->template->title = 'Kohana 3.0 HMVC Test'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework, HMVC'; $this->template->meta_description = 'A test of of the KO3 framework HMVC pattern'; // Fill in content $ko3 = array(); $ko3['posts'] = Request::factory('posts/getposts')->execute()->response; $this->template->content = View::factory('pages/hmvc', $ko3); } }
      
      







「application / classes / controller」フォルダーに「hmvc.php」としてファイルを保存します。



上記のコードはおなじみのはずですが、1行が目立っています。



 $ko3['posts'] = Request::factory('posts/getposts')->execute()->response;
      
      







これは、HMVCの魔法が発生する場所です。 ここで、アクション「getposts」はコントローラー「posts」から呼び出され、取得した結果を将来のビューで渡される配列に保存します。



ビューについて始めたので、このコントローラー用に作成しましょう。



 <?php echo $posts;?>
      
      







これを「hmvc.php」として「application / views / pages /」フォルダーに保存します。



さて、実際には、「投稿」コントローラーを作成しましょう。



 <?php defined('SYSPATH') or die('No direct script access.'); class Controller_Posts extends Controller { public function action_index() { } public function action_getposts() { // Load model $posts = new Model_Post(); // Fill content array for view with last 10 posts. $content = array(); $content['posts'] = $posts->getLastTenPosts(); // Render and output. $this->request->response = View::factory('pages/hmvc_posts', $content); } }
      
      







ファイルを「posts.php」として「application / classes / controller」ディレクトリに保存します。



次に、新しいビューの空のドキュメントを開きます。



 <?php foreach($posts as $post):?> <h1><?php echo $post['title'];?></h1> <?php echo $post['post'];?> <hr /> <?php endforeach;?>
      
      







これを「hmvc_posts.php」として「applications / views / pages」フォルダーに保存します。



ブラウザで「http:// localhost / mykohana3 / hmvc」を開くと、いくつかのエントリが画面に表示されます。 ビューからコントローラーに直接アクセスすることで、同じ結果を得ることができます。 単一のテンプレートを使用するため、このテンプレートを使用するすべてのコントローラーにHMVCを実装する必要はありません。



ファイル「hmvc.php」をフォルダー「application / classes / controller」から次のフォームに移動します。



 <?php defined('SYSPATH') or die('No direct script access.'); class Controller_Hmvc extends Controller_DefaultTemplate { public function action_index() { // Set meta data $this->template->title = 'Kohana 3.0 HMVC Test'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework, HMVC'; $this->template->meta_description = 'A test of of the KO3 framework HMVC pattern'; // Fill in content $ko3 = array(); $ko3['content'] = 'Hello there!'; $this->template->content = View::factory('pages/hmvc', $ko3); } }
      
      







「application / views / pages /」フォルダから「hmvc.php」を編集して、次のようにします。



 <?php echo $content;?><br/> <?php echo Request::factory('posts/getposts')->execute()->response;?>
      
      







これはかなり単純な例で、たとえば、単純なhtml、html、完全なhtml、xml、jsonなどのさまざまな形式でデータを送信できるようにすることで拡張できます。



All Articles