Moony
MoonyϡPHPǵҤ줿̤Webץꥱե졼Ǥ
ƥץ졼ȥȤSmartyѤƤޤ
ʳȯ: Moony::log
ưĶ
- PHP 4.3.xʾ PHP 5.xǤưϤǤ
- mbstring⥸塼뤬ѲǽǤ뤳
- PATH_INFOѲǽǤ뤳
ȡ
pearѤƥȡ뤹ʲ˼ޤ
pearѤʤϡǤդΥǥ쥯ȥŸ֤Ƥ
- Moony-0.10.4.tgzɤޤ
-
ʲΥޥɤ¹Ԥޤ
$ pear install /path/to/Moony-0.10.4.tgz
-
Smartyȡ뤷ƤʤϡʲμǥȡǽǤ
$ pear channel-discover pearified.com $ pear install pearified/Smarty
-
Moonyȡ뤹ϡʲΥޥɤ¹Ԥޤ
$ pear uninstall __uri/Moony
Υե
ǥѥåȤindex.phpѰա/index.php/foo.html˥:
- 饤 index.php ʥǥѥå
- index.php Moony ǥѥå
- Moony Moony_Controller ʥƼ
- Moony_Controller Moony_Router ʥ̾η
- Moony_Controller Moony_Request ʥꥯȥѥǼ
- Moony_Controller Moony_Response ʥ쥹ݥ
- Moony_Controller Moony_Session ʥåϡ
- Moony_Controller Moony_Flash Flashѿꥹȥ
- Moony_Controller Moony_View ʥǥեȥƥץ졼̾Τ
- Moony_Controller Moony_Plugins SmartyΥץ饰
- Moony_Controller Foo
- Moony_Controller Moony_Injector
- Moony_Controller Foo filteråɤ¹ԡ
- Moony_Controller Foo validateåɤ¹ԡ
- Moony_Controller Foo executeåɤ¹ԡ
- Moony_Controller Moony_View ʲ
̾ηˡ
URLư륹ץȤΥޥåԥΤեҤɬפϤޤ
ȤưʲΤ褦˥ޥåԥޤ:
| URL | ̾ | 饹ե | ƥץ졼ȥե |
|---|---|---|---|
| /index.php/foo.html | Foo | Foo.php | Foo.tpl |
| /index.php/foo/bar.html | FooBar | FooBar.php | FooBar.tpl |
| /index.php/foo_bar.html | FooBar | FooBar.php | FooBar.tpl |
| /index.php/foo/bar/param.html | FooBar | FooBar.php | FooBar.tpl |
| /index.php/foo | Foo | Foo.php | Foo.tpl |
| /index.php/foo/ | FooIndex | FooIndex.php | FooIndex.tpl |
| /index.php/foo/index.html | FooIndex | FooIndex.php | FooIndex.tpl |
| /index.php/ | Index | Index.php | Index.tpl |
| /index.php/index.html | Index | Index.php | Index.tpl |
ĥҤϾ̵뤵졢URL2ܤޤǤ̾Ȥưޤ
ʹߤURLʸϡץꥱΥѥȤưޤ
̾ȤưؿϡMoony.phpѹǽǤ
ǥ쥯ȥ˥äϡindex˥äΤȤߤʤޤ
ǥ쥯ȥʤɡưĶ
饹γǼ衢ƥץ졼ȥեγǼ衢 եγǼ衢եνʤɤ줾ꤹ뤳ȤǤޤ Moonyβ줾Υåɤ¹ԤƤ
| å̾ | ǽ | ꤷʤδ |
|---|---|---|
| setActionDir($action_dir) | 饹Ǽǥ쥯ȥ | action |
| setTemplateDir($template_dir) | ƥץ졼ȥեǼǥ쥯ȥ | template |
| setWorkDir($work_dir) | եǼǥ쥯ȥ | work |
| setLogDir($log_dir) | եǥ쥯ȥ | log |
| setTemplateExtension($template_extension) | ƥץ졼ȥեγĥҤ | tpl |
| setLeftDelimiter($left_delimiter) | Smartyκڤʸ | { |
| setRightDelimiter($right_delimiter) | Smartyαڤʸ | } |
顼ν
ɽɬפʥƥץ졼ȥե뤬Ĥʤ硢404Not Foundˤ֤ޤ ޤץꥱǥ顼ȯ硢500Internal Server Errorˤ֤ޤ ξԤޤ
塼ȥꥢ
ǥѥåμ
Smarty.class.phpMoony.php롼ɤޤ MoonyΥɬפ˱ƳƼԤdispatchåɤ¹Ԥޤ
ʲ˼ޤ:
<?php
require_once '/path/to/Moony.php';
require_once '/path/to/Smarty.class.php';
$moony =& new Moony();
$moony->setLeftDelimiter('<{');
$moony->setRightDelimiter('}>');
$moony->dispatch();
?>
饹ΥѿФơ Moony륳ݡͥȤΥޤ ǥեȤǰʲΤ褦˥ޤ ѿѰդƤʤ硢ϹԤޤ
| ѿ̾ | 륯饹 |
|---|---|
| $request | Moony_Request |
| $response | Moony_Response |
| $session | Moony_Session |
| $flash | Moony_Flash |
| $smarty | Smarty |
ޤǤդΥ饹Υ뤳ȤǤޤ Τ褦iniեѰաInjections˥:̾:饹̾Ҥޤ 饹Υեinclude_pathˤơPEAR̿̾§˽äƥޤ Foo_Calc饹ξFoo/Calc.phpMoony_DBMoony_Uploaderˡ Υ⥢饹˥ѿƤʤޤ
[Injections] calc=Foo_Calc db=Moony_DB uploader=Moony_Uploader
iniեϡǥѥåɤ߹Ƥޤ
<?php
require_once '/path/to/Moony.php';
require_once '/path/to/Smarty.class.php';
$moony =& new Moony();
$moony->setConfigFile('/path/to/ini_file');
$moony->dispatch();
?>
饹μʼºݤν
ºݤνexecuteåɤ˵Ҥޤ ä˼Ԥʤ硢饹Τά뤳ȤǽǤ ξ硢˽ä̾ΤΥƥץ졼ȥե뤬ľɽޤ
class Foo
{
function execute()
{
// ºݤν
}
}
饹μʥꥯȥѥͤλѴ
ꥯȥѥͤѴϡ filteråɤ˽ҤޤѥȤMoony_FilterΥϤޤ
class Foo
{
var $request;
function filter($filter)
{
// 'furigana'ͤѥʤѴ
$filter->toKatakana('furigana');
// 'name'ͤ'foo'ä硢'bar'ִ
if ($this->request->get('name') == 'foo') {
$this->request->replace('name', 'bar');
}
}
}
饹μʥꥯȥѥͤΥå
ꥯȥѥͤåϡ validateåɤ˽ҤޤѥȤMoony_ValidatorΥϤޤ 顼줿硢ϸΥƥץ졼ȤɽޤʥåϤƤˡ
POSTäƽԤ饹:
class Register
{
var $request;
function validate($validator)
{
// validation顼λѥƥץ졼Ȥ
$validator->setTemplate('Entry');
// Moony_Validatorεǽ
$validator->setRequired('name', '̾ɬϤƤ');
$validator->setKatakana('furigana', 'եꥬʤϥʤϤƤ');
// ȼڡʥ顼ξ硢顼å
if ($this->request->get('foo_cd') != '1') {
$validator->addError('foo_cd', 'CD꤬ޤ');
}
}
}
validation顼ΤϸΥƥץ졼ȥեܤˡ 顼åɽϥեΥƥץ졼ȤʲΤ褦ˤޤ ܤ˴ؤƤϡMoony_Plugins饹ȤƤ
ϥեΥƥץ졼ȥե:
{moony_errors header='<ul>' footer='</ul>' prefix='<li>' postfix='</li>'}
<form action="/index.php/register" method="POST">
եꥬ: <input type="text" name="furigana" value="{$furigana}" />
̾: <input type="text" name="name" value="{$name}" />
<input type="submit" />
</form>
ꥯȥѥμ
ꥯȥѥMoony_Request饹˳ǼƤޤ Υ饹˳ǼƤΤϡ餫NullХʸ졢ʸǥѴԤ줿ͤǤ
class Foo
{
var $request;
function execute()
{
// 줾
$user = $this->request->get('user');
$pass = $this->request->get('pass');
// ѥץѥƥȤƻĥ֥Ȥ
$obj = $this->request->toObject();
}
}
URLȤϤ줿ѥμ
URLȤϤ줿ѥMoony_Request饹˳ǼƤޤ 㤨С/index.php/foo/bar/param1/param2.htmlȤURLξ:
class FooBar
{
var $request;
function execute()
{
$param1 = $this->request->getPathInfo(0); // ʸ'param1'
$param2 = $this->request->getPathInfo(1); // ʸ'param2'
}
}
å
åˤMoony_Session饹ͳǥޤ
class Foo
{
var $request;
var $session;
function execute()
{
if ($this->session->exists('user')) { // ¸߳ǧ
$user = $this->session->get('user'); //
} else {
$user = $this->request->get('user');
$this->session->set('user', $user); //
}
}
}
Flashѿ
FlashѿϰŪͤǼΰǤMoony_Flash饹ͳǥޤ ºݤͤϥå˳Ǽ졢Υ¹ԤȤޤݻޤ μΥ¹Ԥ륿ߥǡFlashѿ˴ޤ
class Foo
{
var $flash;
function execute()
{
$user = $this->flash->get('user'); //
$this->flash->set('user', $user); //
$this->flash->keep('user'); // Υ˰Ѥ
}
}
ƥץ졼Ȥͤꤹ
Moony_Response饹Ѥޤ
class Foo
{
var $response;
function execute($request, $response)
{
$this->response->set('user', $user); // ͤϥǥեȤǥ
$this->response->setAll($items); // ϢͤϥǥեȤǥ
$this->response->set('user', $user, false); // Ū˥פʤ
$this->response->setAll($items, false); // ϢŪ˥פʤ
}
}
Ѥƥץ졼ȥեѹ
Moony_Response饹Ѥޤ
class Foo
{
var $response;
function execute()
{
$this->response->setTemplate('Another'); // Another.tpl
}
}
Moony_Response饹Ѥޤ ꥯȥѥȥƥץ졼ȤꤷͤϼΥˤѤޤ
class Foo
{
var $response;
function execute()
{
$this->response->setNextAction('NextAction');
}
}
̤URL˥쥯ȤԤ
Moony_Response饹Ѥޤ
class Foo
{
var $response;
function execute()
{
$this->response->redirect('http://example.com/'); // example.com
}
}
桼ФƥեΥɤԤ
Moony_Response饹Ѥޤ
class Foo
{
var $response;
function execute()
{
$this->response->sendFile('/path/to/attachment_file'); // attachment_file
}
}
եΥåץɤԤ
Moony_Uploader饹Ѥޤ
<form action="/index.php/upload" enctype="multipart/form-data" method="POST"> <input type="file" name="userfile" /> <input type="submit" value="send" /> </form>
嵭Τ褦ʥեब硢ʲΤ褦˵Ҥ뤳Ȥǥե륢åץɽԤȤǤޤ iniեMoony_UploaderΥ꤬ԤƤˡ
class Upload
{
var $request;
var $uploader;
function execute()
{
$uploader->setTarget($this->request, 'userfile');
$uploader->setEncoding('SJIS');
if ($uploader->upload('/path/to/save_dir', 'save_file_name', 0644)) {
// åץ
} else {
// åץɼ
}
}
}
եMoony_Uploader饹ꤵ줿硢 Moony_UploaderΥ饹ΥѿȤƥޤ
ñʥƥȤνϤԤ
Moony_Response饹ѤޤMoony_Response::outputʣ¹Ԥ뤳ȤǽǤ 2ܤΰȤʸǥλ꤬ǽǤν¹Ԥ줿硢 ƥץ졼ȤΥϼ¹ԤޤAjaxˤбǽǤ
class Foo
{
var $response;
function execute()
{
$this->response->output('ϤƥȤ', 'UTF-8');
}
}
ȥȡΥåԤ
Moony_RequestѤޤޤեΥƥץ졼Ȥ˰ʲΤ褦˵Ҥޤ
<form action="/index.php/foo" method="POST">
{moony_token xhtml=true} {* XHTMLȤƽϤʤ硢xhtml=trueεҤ *}
</form>
ȡɽ˰ʲΤ褦ŸޤSmartyΥץ饰ǽѡˡ
<form action="/index.php/foo" method="POST"> <input type="hidden" name="_moony_token" value="..." /> </form>ΥեPOSTꥯȤ륢饹ǡȥȡåԤޤ ʲΤ褦˵Ҥ뤳ȤǥåԤȤǤޤ
class Foo
{
var $request;
var $session;
function execute()
{
if ($this->request->checkToken()) {
// ȡ
} else {
// ʥȡ
}
}
}
Moony_DBȤ
Moony_DBPEAR::DBѤñʥǡ١饹Ǥ ץ졼ۥޤSQL¹Ԥ ̤ܤ̾ΤȤϢȤ֤ޤ ޤͤʸǥѴưǹԤޤ
$db =& new Moony_DB();
$db->connect($dsn, 'EUC-JP'); // DBʸǥEUC-JPξ
$result = $db->query('select id,name from user where id=?', array($user_id));
$db->disconnect();
foreach($result as $row) {
print 'id: ' . $row['id'] . ', name: ' . $row['name'] . "\n";
}
եMoony_DB饹ꤵ줿硢 Moony_DBΥ饹ΥѿȤƥޤ
ϤԤ
Moony_Logger饹Ѥ뤳ȤǥƥȥϲǽǤ ޤMoony.phpMOONY_LOG_CATEGORYͤѹ뤳Ȥ ϤԤ٥ͤԤȤǤޤ ʽͤMOONY_LOG_LEVEL_INFOǡdebugϽϤޤ
Moony_Logger::fatal('message'); // ̿Ū顼
Moony_Logger::error('message'); // 顼
Moony_Logger::warn('message'); // ٹ
Moony_Logger::info('message'); // ̾
Moony_Logger::debug('message'); // ǥХå
Moony_Logger::info('message', __FILE__); // ե̾
Moony_Logger::info('message', __FILE__, __LINE__); // եֹ̾