Php AjaX with Templates (PAXT)ってみた件
JavaScriptを書かなくてもAjaxができてテンプレート部分はSmartyだからとっつきやすいPHPのAjaxフレームワークPAXTを試してみましたのでその際のメモ。Hello World!!するアプリ制作の何か。PAXTの動作要件はおそらくPHP5。
配置
PAXTアプリの配置は代替以下の通りになります。templatesとtemplates_cはSmartyで利用するそれです。
[public_html] | +----[sample] | | | +---- index.php5 | | | +---- server.php5 | | | +---- classes.php | | | +---- [templates] | | | +---- [templates_c] | +----[lib] | +----【PAXTのlibの中身】
スケルトン
PAXTアプリを作成するときの骨格は以下の通り。これを改造してHelloWorld!!アプリを作成しました。
index.php5
<? require_once "classes.php"; session_start(); $srv = new server('myindex'); $srv->run(); ?>
server.php5
<? include_once "classes.php"; server::handleServerPart(); ?>
classes.php
<? // require_once "../lib/widget.class.php"; require_once "../lib/embwidget.class.php"; require_once "../lib/server.class.php"; class foo extends embwidget { function __construct($parent=null) { $this->template="launched.html"; if ($parent) { $this->data->variable = $parent->data->variable; } parent::__construct($parent); } } class myindex extends widget { function __construct() { $this->template="myindex.html"; parent::__construct(true); } } ?>
まぁ。詳しいことはhttp://paxt.pagema.net/wiki/quickstartniに書いてあります。
Hello World!!する
というわけで上記のスケルトンに肉付けしていく形でHelloWorld!!するアプリをつくってみました。
テンプレート
さきにView部分のテンプレートから作成。作成完了できたらtemplatesフォルダ以下に配置。今回はindex.htmlとhello.html作成。
index.html
Message : <br /> <div> {embed group="widget001"} </div> <input type="button" value="Hoge" onclick="{$this}.aproxy.onSayHello()" />
hello.html
Hello World!!
classes.php
今回をいじる必要があるのはclasses.phpだけなので他は放置。
<? class hello extends embwidget { function __construct($parent = null, $varname = null) { $this->template = 'hello.html'; if($parent) { $this->varname = $varname; $this->data->state = &$parent->data->$varname; } parent::__construct($parent); } } class myindex extends widget { function __construct() { $this->template = 'index.html'; $this->exportMethod('onSayHello'); //~~~~~~~~~~ // html部分のonSayHello()に対応。 // メソッドonSayHelloHandlerと対応。 $this->embedWidget('widget001', new hello($this, 'hello')); parent::__construct(true); } function onSayHelloHandler() { $this->embedLive("widget001",new hello($this, uniqid("hello"))); // ~~~~~~~~~ index.html {embed group="widget001"}に対応 } } ?>
というわけで
うまくいくとHello World!!できるアプリケーションが完成。難点としてPAXTでアプリを作成するとHTMLコードがすごいことになるので注意が必要かなと…。