Open Source Web Development Tutorials - Dev Shed
CodeIgniterでブログ作成ツールを作ろう
(2009/02/20公開)
簡単なビューファイルの構築
1つ前の部分で、「blogs」と名付けたMySQLテーブルに保存されたブログエントリをすべて表示するためのコントローラクラスの作り方を説明した。もっとも、この動作は、まずはそのブログ関連のデータを実際に表示するためのビューファイルを作らなければ実行することはできない。
それで、下にそのビューファイルの構造を掲載しておく。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $title;?></h1>
<?php foreach($result->result_array() as $blog):?>
<h2><?php echo $blog['title'];?></h2>
<p><?php echo $blog['text'];?></p>
<p><?php echo anchor('blogger/comments/'.$blog['id'],'View Blog Comments >>');?></p>
<?php endforeach;?>
</body>
</html>
このビューファイルは非常に短いものだが、有用なタスクを行っている。いくつか説明しておこう。まず、ご覧のようにこのファイルでは、対応するすべてのブログエントリをそのタイトルとテキストを含め、「foreach」ループを使って表示している。さらに、CodeIgniterのヘルパー関数である「url」を使い、コメント部分へのリンクを動的に作成し、ビューファイルは実行を終える。
もっとも、コメント部分はまだ作っていない。これは先のコントローラクラスにインプリメントしなければならないものだが、その方法についてはこの連載で後ほど触れることにする。今のところは気にしないことにしよう。
このビューファイルの機能を把握していただけたなら、CodeIgniterの/system/application/views/ フォルダに「blogs_view.php」として保存しておこう。
ここまでの部分で、今のところは先に作ったMySQLテーブルに保存されたブログエントリ群を表示する機能のみを持った簡単なブログ用アプリケーションの作り方を説明することができた。
このアプリケーションをテストするには、ブラウザのアドレス部分に以下のURLを入力する。
http://localhost/codeigniter/index.php/blogger/blogs/
すべてうまくできているなら、ブラウザに下のように出力されるはずだ。
This is the title of the first blog
This is the content of the first blog. This is the content of the first blog.
This is the content of the first blog. This is the content of the first blog.
View Blog Comments >>
This is the title of the second blog
This is the content of the second blog. This is the content of the second blog.
This is the content of the second blog. This is the content of the second blog.
View Blog Comments >>
This is the title of the third blog
This is the content of the third blog. This is the content of the third blog.
This is the content of the third blog. This is the content of the third blog.
View Blog Comments >>
This is the title of the fourth blog
This is the content of the fourth blog. This is the content of the fourth blog.
This is the title of the fourth blog. This is the title of the fourth blog.
View Blog Comments >>
This is the title of the fifth blog
This is the content of the fifth blog. This is the content of the fifth blog.
This is the title of the fifth blog. This is the title of the fifth blog.
View Blog Comments >>
This is the title of the sixth blog
This is the content of the sixth blog. This is the content of the sixth blog.
This is the content of the sixth blog. This is the title of the sixth blog.
View Blog Comments >>
This is the title of the seventh blog
This is the content of the seventh blog. This is the content of the seventh blog.
This is the content of the seventh blog. This is the title of the seventh blog.
View Blog Comments >>
This is the title of the eight blog
This is the content of the eight blog. This is the content of the eight blog.
This is the content of the eight blog. This is the title of the eight blog.
View Blog Comments >>
This is the title of the ninth blog
This is the content of the ninth blog. This is the content of the ninth blog.
This is the content of the ninth blog. This is the title of the ninth blog.
View Blog Comments >>
This is the title of the tenth blog
This is the content of the tenth blog. This is the content of the tenth blog.
This is the content of the tenth blog. This is the title of the tenth blog.
View Blog Comments >>
これだけでも、かなりおもしろいものだ。今回述べた通り、このブログ用ツールは「blogs」というMySQLテーブルに保存されたブログエントリをすべて表示することができる。この動作はすべて、1つのコントローラと簡単なビューファイルのみを用いて行われているのだ。
さらに何を求めようか?実はこのブログ用プログラムにはさらに多くの機能を付け足していくことになる。これらはこの連載の続く部分で徐々に組み込んでいこう。今のところは、今回出てきたサンプルコードを復習し、CodeIgniterを使ってデータベース主導型プログラムを開発する方法に慣れていただきたい。
終わりに
連載第1回ではPHPフレームワークである「CodeIgniter」で、基本的なブログ用アプリケーションの作り方を説明した。その作業はご覧いただいた通り、コントローラクラスとビューファイルを各1つずつだけ作るという分かりやすいものだった。
次回は、今回お見せした全ブログエントリを数ページにまたがって扱うために、CodeIgniterに内蔵されたページネートクラスを使う方法を紹介する。次回もお見逃しなく!
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








