<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>Take it easy!　 - mauekusa &#039;s BLOG - &#187; FuelPHP</title>
	<atom:link href="http://blog.bot.vc/category/fuelphp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bot.vc</link>
	<description>　SocialMedia / SmartPhone / Banana な BLOG</description>
	<lastBuildDate>Tue, 28 Apr 2026 18:21:19 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.bot.vc/category/fuelphp/feed/" />
		<item>
		<title>PHPの高速フレームワーク FuelPHP を試してみました。入門版サンプルと、MongoDBの設定方法まとめ</title>
		<link>http://blog.bot.vc/2013/01/fuelphp/</link>
		<comments>http://blog.bot.vc/2013/01/fuelphp/#comments</comments>
		<pubDate>Sat, 12 Jan 2013 13:40:06 +0000</pubDate>
		<dc:creator>manabu uekusa</dc:creator>
				<category><![CDATA[FuelPHP]]></category>
		<category><![CDATA[mongoDB]]></category>

		<guid isPermaLink="false">http://blog.bot.vc/?p=3811</guid>
		<description><![CDATA[この記事の所要時間： 約 1分20秒 FuelPHPは、2011年の7月にバーション1.0がリリースした 最新のPHPフレームワークです。 特徴は、MongoDBやMySQLなどをサポートしていて RESTfulなコント &#8230; <a href="http://blog.bot.vc/2013/01/fuelphp/">続きを読む <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="border-top:2px dotted #ccc;border-bottom:2px dotted #ccc;padding:0.2em 0;text-align:right;font-size:small;color:#444">
	この記事の所要時間： <span style="font-weight:bold">約 <span style="color:#f00">1</span>分<span style="color:#f00">20</span>秒</span>
</div>
<p>
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/fuelPHP_logo.png" title="FuelPHP logo"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/fuelPHP_logo.png" alt="FuelPHP logo" title="fuelPHP_logo.png" border="0" width="550" height="138" /></a><br />
<br />
<br />
<strong>FuelPHP</strong>は、2011年の7月にバーション1.0がリリースした<br />
最新の<strong>PHPフレームワーク</strong>です。<br />
<br />
特徴は、MongoDBやMySQLなどをサポートしていて<br />
RESTfulなコントローラーもすぐに作成できます。<br />
また、<strong>軽量高速</strong>です。<br />
<br />
<strong>FuelPHP公式サイト</strong><br />
 <a href="http://fuelphp.com/" target="_blank" class="advmk">http://fuelphp.com/</a><br />
<br />
実際に、ちょこっとプログラムを書いて、使い方をまとめておきたいと思います。<span id="more-3811"></span>インストールは、とっても簡単です。<br />
下記のコマンドで、さくっとインストールされます。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"># curl get.fuelphp.com/oil | sh</pre></div></div>

<br />
<br />
<br />
次に、動かしたいディレクトリに移動して、コマンドを入力すると<br />
プロジェクトが作成出来ます。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"># oil create 'プロジェクト名'</pre></div></div>

<br />
<br />
<br />
fuelってディレクトリが作りたければ<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"># oil create fuel</pre></div></div>

<br />
<br />
です。<br />
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/fuel_directory.png" title="Fuel directory"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/fuel_directory.png" alt="Fuel directory" title="fuel_directory.png" border="0" width="231" height="515" /></a><br />
<br />
こんな感じにファイルが展開されます。<br />
<br />
http://localhost/fuelPHP/public/<br />
<br />
公開ポイントにアクセスすると、FuelPHPの画面がでます。<br />
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/fuelPHP_top.png" title="FuelPHP top"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/fuelPHP_top.png" alt="FuelPHP top" title="fuelPHP_top.png" border="0" width="400" height="auto" /></a><br />
<br />
主要な設定ファイルは、3つあります。<br />
<strong>fuel/app/config</strong> ディレクトリにあります。<br />
<br />
config.php<br />
メインの設定ファイル<br />
<br />
db.php<br />
データベース設定ファイル<br />
<br />
routes.php<br />
ルーティング設定ファイル<br />
<br />
config.phpを下記のように書き換えます。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">/</span>**</span>
<span style="color: #009900;"> * Fuel is a fast, lightweight, community driven PHP5 framework.</span>
<span style="color: #009900;"> *</span>
<span style="color: #009900;"> * @package    Fuel</span>
<span style="color: #009900;"> * @<span style="color: #000066;">version</span>    <span style="color: #cc66cc;">1.0</span></span>
<span style="color: #009900;"> * @author     Fuel Development Team</span>
<span style="color: #009900;"> * @license    MIT License</span>
<span style="color: #009900;"> * @copyright  <span style="color: #cc66cc;">2010</span> - <span style="color: #cc66cc;">2012</span> Fuel Development Team</span>
<span style="color: #009900;"> * @<span style="color: #000066;">link</span>       http:<span style="color: #66cc66;">//</span>fuelphp.com</span>
<span style="color: #009900;"> *<span style="color: #66cc66;">/</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #66cc66;">/</span>**</span>
<span style="color: #009900;"> * If you want to override the default configuration, add the keys you</span>
<span style="color: #009900;"> * want to change here, and assign new values to them.</span>
<span style="color: #009900;"> *<span style="color: #66cc66;">/</span></span>
&nbsp;
<span style="color: #009900;">return array<span style="color: #66cc66;">&#40;</span></span>
<span style="color: #009900;"><span style="color: #ff0000;">'index_file'</span> <span style="color: #66cc66;">=</span>&gt;</span> false,
'profiling' =&gt; false
);</pre></div></div>

<br />
<br />
<br />
<strong><a href="http://example.jp/index.php/bbs/" target="_blank" class="advmk">http://example.jp/index.php/bbs/</a></strong>を <strong><a href="http://example.jp/bbs/" target="_blank" class="advmk">http://example.jp/bbs/</a></strong>で<br />
アクセス可能になります。<br />
<br />
.htaccessで、mod_rewriteの設定がされています。<br />
<br />
<h2>Hello Worldの表示</h2>
<br />
やっぱり、はじめのプログラムと言えば、<strong>Hello World</strong>の表示なので<br />
サンプルを作ってみます。<br />
<br />
まず、<strong>routes.php</strong>のhelloの記述をコメントアウトします。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;">return array<span style="color: #66cc66;">&#40;</span></span>
<span style="color: #009900;">	<span style="color: #ff0000;">'_root_'</span>  <span style="color: #66cc66;">=</span>&gt;</span> 'welcome/index',  // The default route
	'_404_'   =&gt; 'welcome/404',    // The main 404 route
&nbsp;
//	'hello(/:name)?' =&gt; array('welcome/hello', 'name' =&gt; 'hello'),
);</pre></div></div>

<br />
<br />
<br />
<strong>fuel/app/classes/controller/hello.php</strong> ファイルを作成します。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000066;">class</span> Controller_Hello extends Controller</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">	public function action_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">		return <span style="color: #ff0000;">'Hello World!!'</span>;</span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span></pre></div></div>

<br />
http://localhost/fuelphp/public/hello/index<br />
<br />
にアクセスすると、<strong>Hello World!!</strong>が表示されます。<br />
<br />
<h3>実行結果</h3>
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/helloworld.png" title="Helloworld"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/helloworld.png" alt="Helloworld" title="helloworld.png" border="0" width="319" height="63" /></a><br />
<br />
このサンプルは、Controllerだけで表示しているので<br />
次は、Viewを使った表示にしてみます。<br />
<br />
<strong>fuel/app/views/hello.php</strong> ファイルを作成します。<br />
<strong>View</strong>だけ場所が違うので、始めは戸惑います＾＾<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Hello world<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Hello World!!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<br />
<br />
<br />
<strong>fuel/app/classes/controller/hello.php</strong> 先ほどのコントローラーファイルを書き換えます。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #000066;">class</span> Controller_Hello extends Controller</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">	public function action_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">		return View::forge<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'hello'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span></pre></div></div>

<br />
http://localhost/fuelphp/public/hello/index<br />
<br />
に再度アクセスすると、<strong>Hello World!!</strong>が表示されます。<br />
<br />
<h3>実行結果</h3>
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/helloworld.png" title="Helloworld"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/helloworld.png" alt="Helloworld" title="helloworld.png" border="0" width="319" height="63" /></a><br />
<br />
結果は同じですが、<strong>View</strong>を使った表示になっています。<br />
<br />
<h2>MVCモデル</h2>
コードをモデル (<strong>Model</strong>)、ビュー (<strong>View</strong>)、コントローラ (<strong>Controller</strong>) の 3つの役割に分割して作成します。<br />
<br />
<strong>コントローラー</strong>は、<strong>アプリケーション全体の制御</strong>をします。<br />
<strong>モデル</strong>は、<strong>データの管理</strong>をします。<br />
<strong>ビュー</strong>は、<strong>画面表示</strong>を行います。<br />
<br />
それぞれ、MVCに分けてコーディングしていきます。<br />
<br />
<h2>URLの構造</h2>
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">http://example.jp/コントローラ/メソッド [/パラメータ 1[/パラメータ 2[/...]]]</pre></div></div>

<br />
<br />
<br />
<strong>1番目</strong>の URI セグメントが、実行される<strong>コントローラ名</strong><br />
<strong>2番目</strong>の URI セグメントが、実行されるコントローラの<strong>メソッド名</strong><br />
<strong>3番目以降</strong>の URI セグメントが、メソッドに渡される<strong>引数</strong><br />
<br />
<strong>fuel/app/classes/controller/blog.php</strong> ファイルを作成します。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #000066;">class</span> Controller_Blog extends Controller <span style="color: #66cc66;">&#123;</span></span>
&nbsp;
<span style="color: #009900;">	public function action_category<span style="color: #66cc66;">&#40;</span>$cat <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'php'</span>,$page <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">		return __FILE__ . <span style="color: #ff0000;">'&lt;br /&gt;</span></span>' . $cat . '<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>' . $page;
	}
}</pre></div></div>

<br />
http://localhost/fuelphp/public/blog/category/php/1<br />
<br />
<h3>実行結果</h3>
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/php1.png" title="Php1"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/php1.png" alt="Php1" title="php1.png" border="0" width="499" height="90" /></a><br />
<br />
http://localhost/fuelphp/public/blog/category/perl/2<br />
<br />
<h3>実行結果</h3>
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/perl2.png" title="Perl2"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/perl2.png" alt="Perl2" title="perl2.png" border="0" width="487" height="91" /></a><br />
<br />
それぞれに、<strong>引数</strong>の内容が表示されています。<br />
<br />
<h2>Restコントローラー</h2>
<br />
<strong>fuel/app/classes/controller/weather.php</strong> ファイルを作成します。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #000066;">class</span> Controller_Weather extends Controller_Rest</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">	public function get_today<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">		<span style="color: #66cc66;">//</span>クエリ文字列から地名を代入</span>
<span style="color: #009900;">		$location <span style="color: #66cc66;">=</span> Input::get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'loc'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">		$weather <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'fine'</span>;</span>
&nbsp;
<span style="color: #009900;">		<span style="color: #66cc66;">//</span>レスポンスを返す</span>
<span style="color: #009900;">		$this-&gt;</span>response(array(
			'location' =&gt; $location,
			'weather' =&gt; $weather,
		));
	}
}</pre></div></div>

<br />
<br />
<br />
パラメータを付けてアクセスします。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">http://localhost/fuelphp/public/weather/today.json?loc=tokyo</pre></div></div>

<br />
<br />
<br />
<h3>実行結果</h3>
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/rest.png" title="Rest"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/rest.png" alt="Rest" title="rest.png" border="0" width="320" height="68" /></a><br />
<br />
<strong>Restコントローラー</strong>を使うと<br />
簡単に、<strong>json</strong>や<strong>xml</strong>、<strong>CSV</strong>で出力することが出来ます。<br />
とっても便利です！<br />
<br />
拡張子を指定すると、自動で指定したフォーマットで出力されます。<br />
xml　　　　XML<br />
json　　　JSON<br />
jsonp　　JSONP<br />
csv　　　CSVファイル<br />
<br />
<br />
<h2>MongoDB</h2>
<br />
<strong>app/config/db.php </strong>の設定をします。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">/</span>**</span>
<span style="color: #009900;"> * Use this file to override global defaults.</span>
<span style="color: #009900;"> *</span>
<span style="color: #009900;"> * See the individual environment DB configs <span style="color: #000066;">for</span> specific config information.</span>
<span style="color: #009900;"> *<span style="color: #66cc66;">/</span></span>
&nbsp;
&nbsp;
<span style="color: #009900;">return array<span style="color: #66cc66;">&#40;</span></span>
<span style="color: #009900;">	<span style="color: #ff0000;">'active'</span> <span style="color: #66cc66;">=</span>&gt;</span> 'mongo',
	'mongo' =&gt; array(
		'default' =&gt; array(
			'hostname'   =&gt; 'localhost',
			'database'   =&gt; 'blog',
			'username'   =&gt; 'user',
			'password'   =&gt; 'pass',
		),
	),
);</pre></div></div>

<br />
<br />
<br />
<strong>fuel/app/classes/controller/blog.php</strong> ファイルを変更します。<br />
<br />

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #000066;">class</span> Controller_Blog extends Controller <span style="color: #66cc66;">&#123;</span></span>
&nbsp;
<span style="color: #009900;">	public function action_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#123;</span></span>
&nbsp;
<span style="color: #009900;">		$mongodb <span style="color: #66cc66;">=</span> \Mongo_Db::instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">		<span style="color: #66cc66;">//</span> 挿入</span>
<span style="color: #009900;">		$results <span style="color: #66cc66;">=</span> $mongodb-&gt;</span>insert('posts', array('name' =&gt; '名前', 'contents' =&gt; 'コンテンツ'));
		// データ取得
		$results = $mongodb-&gt;get('posts');
		var_dump($results);
&nbsp;
		return $results;
&nbsp;
	}
}</pre></div></div>

<br />
http://localhost/fuelphp/public/blog/<br />
<br />
<h3>実行結果</h3>
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/mongo.png" title="Mongo"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/mongo.png" alt="Mongo" title="mongo.png" border="0" width="400" height="auto" /></a><br />
<br />
簡単に、<strong>MongoDB</strong>にも接続できます。<br />
<br />
<h2>まとめ</h2>
<br />
<strong>FuelPHP</strong>などのフレームワークを使うと<br />
<strong>保守性</strong>も上がるし、<strong>モジュールの流用</strong>なども簡単にできるようになります。<br />
<br />
また、jsonなどの<strong>Restコントローラー</strong>は、簡単に作成出来るので<br />
わざわざコーディングしなくても良くなります。<br />
<br />
<strong>脆弱性</strong>の対応なども、フレームワーク側で吸収できます。<br />
<strong>DB</strong>の接続なども簡単なので、FuelPHPを使っていきたいと思います♪<br />
<br />
<h2>参考図書</h2>
<br />
<a rel="lightbox" href="http://blog.bot.vc/wp-content/uploads/2013/01/cover_s.jpeg" title="Cover s"><img src="http://blog.bot.vc/wp-content/uploads/2013/01/cover_s.jpeg" alt="Cover s" title="cover_s.jpeg" border="0" width="100" height="150" /></a><br />
<br />
<strong>はじめてのフレームワークとしてのFuelPHP</strong><br />
 <a href="http://tatsu-zine.com/books/fuelphp1st" target="_blank" class="advmk">http://tatsu-zine.com/books/fuelphp1st</a></p>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:comments href="http://blog.bot.vc/2013/01/fuelphp/"></fb:comments>]]></content:encoded>
			<wfw:commentRss>http://blog.bot.vc/2013/01/fuelphp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.bot.vc/2013/01/fuelphp/" />
	</item>
	</channel>
</rss>
