MicroMVC – クイックスタート
MicroMVCとは
PHPのMVCフレームワーク。
A blazing fast Model View Controller framework in less than 150kb!
超早いMVCフレームワーク。フレームワークサイズは150kb以下!!
インストール~セットアップ
1.ダウンロード
githubサイトから、アーカイブファイルをダウンロードして展開。

2.設置
PHPが実行できるパスにおく(今回はXAMPP環境で「d:\xampp\htdocs\micromvc\」に配置)

MicroMVC設定ファイル(config.php)のコピー
[インストールフォルダ]/localhost/config.sample.phpをコピー。ファイル名は、[インストールフォルダ]/localhost/config.php

MicroMVC設定ファイル(config.php)の記載を修正
279行目、サイトのURLを設置したディレクトリ名にあわせる(今回は、micromvcディレクトリに設置したので修正なし。)
// Absolute URL path to the site root. Default ("/")
define('SITE_URL', '/micromvc/');
localhostのフォルダ名を変更
[インストールディレクトリ]/localhostディレクトリを、設置するドメイン名と同一のものにする。
公式サイトより。
If your site is http://mysite.com then you need to rename the folder to mysite.com. If you are using a subdomain like http://mysite.somesite.com then you need to name the folder mysite.somesite.com.
訳
サイトがhttp://mysite.comなら、フォルダ名はmysite.comとする。サブドメインを利用してhttp://mysite.somesite.comで運用する場合は、ディレクトリ名はmysite.somesite.comとする。
今回は、ローカルホストで動かすので、そのまま。
.htaccessの修正
インストールディレクトリにある、sample.htaccessファイルを.htaccessにリネーム

内容を修正する。
36行目、RewriteBaseの部分を運用するURLにあわせる(config.phpのSITE_URLと同一)
# The RewriteBase of the system (change if you are using this sytem in a sub-folder). RewriteBase /
↓
# The RewriteBase of the system (change if you are using this sytem in a sub-folder). RewriteBase /micromvc/
設定完了。
http://localhost/micromvc/にアクセス。
PEAR – Text_CAPTCHA
Pear::Text_CAPTCHA。PearのCAPTCHA出力用モジュールを使ってみる。
インストール
例によって安定版が存在しないのでアルファ版をインストール
Failed to download pear/Text_CAPTCHA within preferred state “stable”, latest rel
ease is version 0.4.0, stability “alpha”, use “channel://pear.php.net/Text_CAPTC
HA-0.4.0″ to install
install failed
D:\>pear install Text_CAPTCHA-alpha
WARNING: channel “pear.php.net” has updated its protocols, use “pear channel-upd
ate pear.php.net” to update
Did not download optional dependencies: pear/Numbers_Words, pear/Text_Figlet, pe
ar/Image_Text, use –alldeps to download automatically
pear/Text_CAPTCHA can optionally use package “pear/Numbers_Words”
pear/Text_CAPTCHA can optionally use package “pear/Text_Figlet”
pear/Text_CAPTCHA can optionally use package “pear/Image_Text” (recommended vers
ion 0.6.0beta)
downloading Text_CAPTCHA-0.4.0.tgz …
Starting to download Text_CAPTCHA-0.4.0.tgz (14,833 bytes)
…..done: 14,833 bytes
downloading Text_Password-1.1.1.tgz …
Starting to download Text_Password-1.1.1.tgz (4,357 bytes)
…done: 4,357 bytes
install ok: channel://pear.php.net/Text_Password-1.1.1
install ok: channel://pear.php.net/Text_CAPTCHA-0.4.0
D:\>
PEAR::Image_Textもインストール
WARNING: channel “pear.php.net” has updated its protocols, use “pear channel-upd
ate pear.php.net” to update
downloading Image_Text-0.6.0beta.tgz …
Starting to download Image_Text-0.6.0beta.tgz (60,924 bytes)
……………done: 60,924 bytes
install ok: channel://pear.php.net/Image_Text-0.6.0beta
クイックスタート
サンプルコードの実行
<?php
require_once 'Text/CAPTCHA.php';
// CAPTCHA のオプションを設定します (フォントが存在する必要があります!)
$imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#DDFF99',
'lines_color' => '#CCEEDD',
'background_color' => '#555555'
);
// CAPTCHA のオプションを設定します
$options = array(
'width' => 200,
'height' => 80,
'output' => 'png',
'imageOptions' => $imageOptions
);
// 新しい Text_CAPTCHA オブジェクトを Image ドライバで作成します
$c = Text_CAPTCHA::factory('Image');
$retval = $c->init($options);
if (PEAR::isError($retval)) {
printf('CAPTCHA 作成時にエラー: %s!',
$retval->getMessage());
exit;
}
// CAPTCHA のパスフレーズを取得します
$_SESSION['phrase'] = $c->getPhrase();
// CAPTCHA 画像を (PNG 形式で) 取得します
$png = $c->getCAPTCHAAsPNG();
if (PEAR::isError($png)) {
echo 'CAPTCHA 作成時にエラー!';
echo $png->getMessage();
exit;
}
file_put_contents(md5(session_id()) . '.png', $png);
?>
おこられ

フォントがないのが原因ぽい(8行目の「’font_file’=>’COUR.TTF’」)
とりあえず、IPAフォントをダウンロード。(IPAフォント)ipag.ttfをソースコードと同一ディレクトリにおいて、8行目のフォント指定をipag.ttfに修正して再実行・・・画面真っ白。
と思ったら、ソースコードのディレクトリに「’ランダムな文字列.png’」ファイルができてた。

簡単。
PEAR – Calendar
PEAR::Calendar。PEARのカレンダーモジュールを使ってみる。
インストール
パッケージのインストール。
安定版が存在しないのでベータ版をダウンロードする。
Failed to download pear/calendar within preferred state “stable”, latest release
is version 0.5.5, stability “beta”, use “channel://pear.php.net/calendar-0.5.5″
to install
install failed
D:\xampp\php>pear install calendar-beta
WARNING: channel “pear.php.net” has updated its protocols, use “pear channel-upd
ate pear.php.net” to update
Did not download optional dependencies: pear/Date, use –alldeps to download aut
omatically
pear/Calendar can optionally use package “pear/Date”
downloading Calendar-0.5.5.tgz …
Starting to download Calendar-0.5.5.tgz (58,159 bytes)
…………..done: 58,159 bytes
install ok: channel://pear.php.net/Calendar-0.5.5
D:\xampp\php>
サンプルコードの実行
公式ページのサンプルスクリプト(お急ぎの方用 — お湯をかけるだけのように…)を実行してみる
<?php
require_once 'Calendar/Month/Weekdays.php';
$month = new Calendar_Month_Weekdays(date('Y'), date('n'));
$month->build();
echo "<table>\n";
while ($day = $Month->fetch()) {
if ($day->isFirst()) {
echo "<tr>\n";
}
if ($day->isEmpty()) {
echo "<td> </td>\n";
} else {
echo '<td>'.$day->thisDay()."</td>\n";
}
if ($day->isLast()) {
echo "</tr>\n";
}
}
echo "</table>\n";
?>
結果

おー、簡単。
実際に使うときは、テンプレートエンジン(Smartyとか)と組み合わせたほうがスッキリする。
年月の取得
require_once 'Calendar/Month/Weekdays.php';
$month = new Calendar_Month_Weekdays(date('Y'), date('n'));
$month->build();
// (略)
print $month->year;
print $month->month;
カレンダーの開始曜日を設定する
$month = new Calendar_Month_Weekdays(date("Y"), date("n"), 0);
コンストラクタに3つめの引数を設定して制御する。0=日曜日,1=月曜日…6=土曜日。なぜか定数はなし。
週間カレンダーの作成・取得
require_once "Calendar/Week.php";
$week = new Calendar_Week(date("Y"), date("n"), date("d") ,0);
$week->build();
while ($day = $week->fetch()) {
print_r($day->day);
print "<hr>";
}
?>
結果

何週目か取得(ドキュメント)
$week->thisWeek("n_in_month");


