ブログ移転のお知らせ
ここ最近撮影した写真
最近毎日どこかで写真を撮ってる。
その理由は最近App Storeでゲットした「TiltShiftGen」というアプリケーションにハマってる。
Zend framework captchaを試してみた。
何度か試してるけどブログにエントリーしてなかったから
これを機に、エントリーしてみます。
画像認証コントローラー
ExampleCaptchaController.php
<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Session/Namespace.php';
require_once 'Zend/Captcha/Figlet.php';
require_once 'Zend/Captcha/Image.php';
/**
* 画像認証用コントローラ
* @author sackam
*/
class ExampleCaptchaController extends Zend_Controller_Action{
private $_req;
private $_alert;
private $_captcha;
public function init(){
//ViewHelperの設定
$this->view->setHelperPath('Zend/View/Helper.php', 'Zend_View_Helper');
$this->view->doctype('XHTML1_TRANSITIONAL');
$this->_req = $this->getRequest();
$this->_captcha = new Zend_Captcha_Image(array(
'name' => 'captcha_values',
'wordLen' => 6,
'timeout' => 600,
'font' => APP . "views/scripts/example-captcha/Blindfold.ttf",
'imgdir' => "/home/sackam/www//labs/zf/example-img/",
'imgurl' => "/labs/zf/example-img/",
'dotNoiseLevel' => 40,
'lineNoiseLevel' => 3));
}
//indexアクション
public function indexAction(){
/*
$captcha = new Zend_Captcha_Figlet(array(
'name' => 'foo',
'wordLen' => 6,
'timeout' => 300));
*/
$id = $this->_captcha->generate();
$this->view->captchaId = $id;
$this->view->captcha = $this->_captcha;
}
/**
* 確認画面アクション
* @return void
*/
public function confirmAction(){
$exampleCaptcha['nick_name'] = $this->_req->getPost('nick_name');
$exampleCaptcha['captcha_values'] = $this->_req->getPost('captcha_values');
$this->view->exampleCaptcha = $exampleCaptcha;
//入力値チェック
if($this->isValidate($exampleCaptcha)){
$this->view->alert = $this->_alert;
//入力値が間違っているので、入力画面へ転送
$this->_forward('index');
}
}
/**
* 入力値チェック
* エラーが存在する場合はtrue、エラーが存在しない場合はfalseを返す
* @param $inputData
* @return boolean
*/
private function isValidate($inputData){
$error = false;
if($inputData['nick_name'] == ''){
$error = true;
$this->_alert['nick_name_error'] = 'ニックネームは必須項目です。';
//画像認証
}else if(!$this->_captcha->isValid($inputData['captcha_values'])){
$error = true;
$this->_alert['captcha_values_error'] = '画像認証に失敗しました';
}
return $error;
}
}
次に入力画面
index.phtml
<!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=UTF-8" />
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
<title>example</title>
</head>
<body id="wrapper">
<div id="header">
<h1>Example captcha.</h1>
</div>
<?php if(count($this->alert) > 0){?>
<div id="error">
<ul>
<?php foreach($this->alert as $data){?>
<li><?php echo $data?></li>
<?php }?>
</ul>
</div>
<?php }?>
<div id="main">
<h2>Example form.</h2>
<form id="input-form" method="post" action="/labs/zf/example-captcha/confirm">
<dl id="input-dl">
<dt>ニックネーム</dt>
<dd><?php echo $this->formText('nick_name', $this->exampleCaptcha['nick_name'])?></dd>
<dt>画像に表示されている文字を入力してください</dt>
<dd>
<?php echo $this->captcha->render()?>
<?php echo $this->formText('captcha_values[input]', null)?></dd>
</dl>
<input type="submit" name="confirm" id="confirm" value="確認" />
<?php echo $this->formHidden('captcha_values[id]', $this->captchaId)?>
</form>
</div>
<div id="footer">
<address>©2009 Sackam.com</address>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
次に画像認証ができた時の確認画面
confirm.phtml
<!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=UTF-8" />
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
<title>example</title>
</head>
<body id="wrapper">
<div id="header">
<h1>Example captcha.</h1>
</div>
<div id="main">
<h2>Example form.</h2>
<dl id="input-dl">
<dt>ニックネーム</dt>
<dd><?php echo $this->escape($this->exampleCaptcha['nick_name'])?></dd>
</dl>
<p>
画像認証は成功です。
</p>
<a href="/labs/zf/example-captcha">Example captchaに戻る</a>
</div>
<div id="footer">
<address>©2009 Sackam.com</address>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
まぁこんな感じで、サンプルはこちら
Zend framework 入力関連の話2
久しぶりの投稿です。
先の投稿でZend frameworkの入力関連の話に触れました。
またまた入力関連の話です。
今度は、記事の登録、一覧、修正機能について、投稿します。
まずは基本となるコントローラー。
ExampleController.php
<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Session/Namespace.php';
require_once APP . 'models/MessagesModel.class.php';
/**
*
* @author sackam
*/
class ExampleController extends Zend_Controller_Action{
private $_db;
private $_req;
private $_messages;
private $_alert;
private $_referenceId = array(
'1' => '参考になった',
'2' => '参考にならなかった',
'3' => 'どちらでもない');
public function init(){
//データベース設定値の読み込み
$params = array('dbname' => APP . 'models/example.sqlite');
//ViewHelperの設定
$this->view->setHelperPath('Zend/View/Helper.php', 'Zend_View_Helper');
$this->view->doctype('XHTML1_TRANSITIONAL');
//DBの接続
$this->_db = Zend_Db::factory('Pdo_sqlite', $params);
$this->_req = $this->getRequest();
$this->_messages = new MessagesModel($this->_db);
}
//indexアクション
public function indexAction(){
$this->_forward('list');
}
/**
* 一覧画面用アクション
* @return void
*/
public function listAction(){
$this->view->messages = $this->_messages->select();
}
/**
* 入力画面アクション
* @return void
*/
public function inputAction(){
$example['nick_name'] = $this->_req->getPost('nick_name');
$example['title'] = $this->_req->getPost('title');
$example['message'] = $this->_req->getPost('message');
$example['reference_id'] = $this->_req->getPost('reference_id');
$example['id'] = $this->_req->getPost('id');
//編集モードか判定する。
if($this->_req->getUserParam('id') != '' && is_numeric($this->_req->getUserParam('id'))){
$message = new MessagesModel($this->_db);
if($result = $message->select($this->_req->getUserParam('id'))){
$example = $result[0];
}
}
$this->view->referenceId = $this->_referenceId;
$this->view->example = $example;
}
/**
* 確認画面アクション
* @return void
*/
public function confirmAction(){
$example['nick_name'] = $this->_req->getPost('nick_name');
$example['title'] = $this->_req->getPost('title');
$example['message'] = $this->_req->getPost('message');
$example['reference_id'] = $this->_req->getPost('reference_id');
$example['id'] = $this->_req->getPost('id');
//入力値のチェック(不十分)
if($this->isValidate($example)){
$this->view->alert = $this->_alert;
$this->_forward('input');
}else{
$this->view->referenceName = $this->_referenceId[$example['reference_id']];
}
$this->view->example = $example;
}
/**
* 詳細画面アクション
* @return void
*/
public function detailAction(){
$id = $this->_req->getUserParam('id');
if($id != '' && is_numeric($id)){
try{
$result = $this->_messages->select($id);
$example = $result[0];
$this->view->example = $example;
$this->view->referenceName = $this->_referenceId[$example['reference_id']];
}catch(Zend_Exception $e){
die($e->getMessage());
}
}else{
$this->_redirect('example');
}
}
/**
* 登録処理アクション
* @return void
*/
public function registAction(){
$example['nick_name'] = $this->_req->getPost('nick_name');
$example['title'] = $this->_req->getPost('title');
$example['message'] = $this->_req->getPost('message');
$example['reference_id'] = $this->_req->getPost('reference_id');
$id = $this->_req->getPost('id');
//入力値のチェック
if($this->isValidate($example)){
$this->view->alert = $this->_alert;
$this->_forward('input');
//編集モードか判定する(不十分)
}else if($id != '' && is_numeric($id)){
try{
$this->_db->beginTransaction();
$this->_messages->update($id, $example);
$this->_db->commit();
}catch(Zend_Exception $e){
$this->_db->rollBack();
die($e->getMessage());
}
}else{
$this->_messages->insert($example);
}
$this->_redirect('example');
}
/**
* 入力値チェック
* エラーが存在する場合はtrue、エラーが存在しない場合はfalseを返す
* @param $inputData
* @return boolean
*/
private function isValidate($inputData){
$error = false;
if($inputData['nick_name'] == ''){
$error = true;
$this->_alert['nick_name_error'] = 'ニックネームは必須項目です。';
}
if($inputData['title'] == ''){
$error = true;
$this->_alert['title_error'] = 'タイトルは必須項目です。';
}
if($inputData['message'] == ''){
$error = true;
$this->_alert['message_error'] = 'メッセージは必須項目です。';
}
return $error;
}
}
基本的にGETやPOSTのデータとDBから取得してきたデータを扱う際は、コントローラー名のControllerを抜いた連想配列に格納する様にしてます。
(俺流)
次に登録情報一覧画面
list.phtml
<!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=UTF-8" />
<title>example</title>
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
<style type="text/css">
<!--
.amend a{
color:#FFF;
}
-->
</style>
</head>
<body id="wrapper">
<div id="header">
<h1>Zend framework Example.</h1>
</div><!-- End header -->
<div id="navi">
<ul>
<li><a href="/labs/zf/example/input">Add message.</a></li>
</ul>
</div>
<?php if(count($this->alert) > 0){?>
<div id="alert">
<ul>
<?php foreach($this->alert as $data){?>
<li><?php echo $data?></li>
<?php }?>
</ul>
</div>
<?php }?>
<div id="main">
<h2>Example form.</h2>
<form id="input-form" action="/labs/zf/example/delete" method="post">
<?php if(count($this->messages) > 0){?>
<div id="list">
<table>
<thead>
<tr>
<th scope="col"> </th>
<th scope="col">Nick name</th>
<th scope="col">Title</th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<?php foreach($this->messages as $data){?>
<tr>
<td> </td>
<td><?php echo $data['nick_name']?></td>
<td><a href="/labs/zf/example/<?php echo $data['id']?>"><?php echo $data['title']?></a></td>
<td class="amend"><a href="/labs/zf/example/input/<?php echo $data['id']?>">編集</a></td>
</tr>
<?php }?>
</tbody>
</table>
</div>
<?php }else{?>
Message not found.
<?php }?>
</form><br />
</div><!-- End main -->
<div id="footer">
<address>©2009 sackam.com</address>
</div><!-- End footer -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
入力画面
input.phtml
<!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=UTF-8" />
<title>example</title>
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
</head>
<body id="wrapper">
<div id="header">
<h1>Zend framework Example.</h1>
</div>
<div id="navi">
<ul>
<li><a href="/labs/zf/example">Message list.</a></li>
</ul>
</div>
<?php if(count($this->alert) > 0){?>
<div id="error">
<ul>
<?php foreach($this->alert as $data){?>
<li><?php echo $data?></li>
<?php }?>
</ul>
</div>
<?php }?>
<div id="main">
<h2>Example form.</h2>
<form id="input-form" action="/labs/zf/example/confirm" method="post">
<dl id="input-dl">
<dt>Nick name.</dt>
<dd><?php echo $this->formText('nick_name', $this->example['nick_name'])?></dd>
<dt>Title.</dt>
<dd><?php echo $this->formText('title', $this->example['title'])?></dd>
<dt>Message.</dt>
<dd><?php echo $this->formTextarea('message', $this->example['message'], array('rows' => 10, 'cols' => 50))?></dd>
<dt>どうだった?</dt>
<dd><?php echo $this->formSelect('reference_id', $this->example['reference_id'], null, $this->referenceId)?></dd>
</dl>
<input type="submit" name="confirm" id="confirm" value="確認" />
<input type="button" name="cancel" id="cancel" value="キャンセル" onclick="location.href='/labs/zf/example'" />
<?php echo $this->formHidden('id', $this->example['id'])?>
</form>
</div>
<div id="footer">
<address>©2009 Sackam.com</address>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
UserParamが来ていない場合は新規追加。
UserParamが来ている場合は編集機能なのでDBからデータを取得。
確認画面
confirm.phtml
<!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=UTF-8" />
<title>example</title>
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
<script type="text/javascript">
//<![CDATA[
function onBack(){
var f = document.getElementById("input-form");
f.action = "/labs/zf/example/input";
f.submit();
}
//]]>
</script>
</head>
<body id="wrapper">
<div id="header">
<h1>Zend framework Example.</h1>
</div>
<div id="navi">
<ul>
<li><a href="/labs/zf/example">Message list.</a></li>
</ul>
</div>
<?php if(count($this->alert) > 0){?>
<div id="error">
<ul>
<?php foreach($this->alert as $data){?>
<li><?php echo $data?></li>
<?php }?>
</ul>
</div>
<?php }?>
<div id="main">
<h2>Example form.</h2>
<form id="input-form" action="/labs/zf/example/regist" method="post">
<dl id="input-dl">
<dt>Nick name.</dt>
<dd><?php echo $this->escape($this->example['nick_name'])?></dd>
<dt>Title.</dt>
<dd><?php echo $this->escape($this->example['title'])?></dd>
<dt>Message.</dt>
<dd><?php echo nl2br($this->escape($this->example['message']))?></dd>
<dt>どうだった?</dt>
<dd><?php echo $this->escape($this->referenceName)?></dd>
</dl>
<input type="submit" name="regist" id="regist" value="登録" />
<input type="button" name="back" id="back" value="入力画面に戻る" onclick="onBack()" />
<?php echo $this->formHidden('nick_name', $this->example['nick_name'])?>
<?php echo $this->formHidden('title', $this->example['title'])?>
<?php echo $this->formHidden('message', $this->example['message'])?>
<?php echo $this->formHidden('reference_id', $this->example['reference_id'])?>
<?php echo $this->formHidden('id', $this->example['id'])?>
</form>
</div>
<div id="footer">
<address>©2009 Sackam.com</address>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
前回紹介した記事と比べると、form action部分です。
以前紹介した記事だと、そのままcompleteActionに渡していたのですが、さかた、成長したのか。
Javascriptを利用して、form内のactionを書き換えた後、そのフォームをJavascriptを使って送信しています。
そうすると、アドレスがなかなか美しくなった感じになります。
更にコントローラー部分も結構シンプルになります。
更に言うなれば、編集時も同じアクションで実行する事ができます。
※これが本当にイイかは、また別のお話し。
詳細画面
detail.phtml
<!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=UTF-8" />
<title>example</title>
<link rel="stylesheet" href="/labs/zf/common/css/index.css" />
</head>
<body id="wrapper">
<div id="header">
<h1>Zend framework Example.</h1>
</div>
<div id="navi">
<ul>
<li><a href="/labs/zf/example">Message list.</a></li>
<li><a href="/labs/zf/example/input">Add Message.</a></li>
</ul>
</div>
<div id="main">
<h2>Example form.</h2>
<dl id="input-dl">
<dt>Nick name.</dt>
<dd><?php echo $this->escape($this->example['nick_name'])?></dd>
<dt>Title.</dt>
<dd><?php echo $this->escape($this->example['title'])?></dd>
<dt>Message.</dt>
<dd><?php echo nl2br($this->escape($this->example['message']))?></dd>
<dt>どうだった?</dt>
<dd><?php echo $this->escape($this->referenceName)?></dd>
</dl>
<a href="/labs/zf/example">Back to message list.</a>
</div>
<div id="footer">
<address>©2009 Sackam.com</address>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4971938-3");
pageTracker._trackPageview();
</script>
</body>
</html>
ここはただ純粋にUserParamで来た値を基にDBからデータを取得。
サンプルはこちら。
DBはSqliteです。
ちなみにindex.phpの一部を抜粋したものはこのとおり。
Router_Routeを使ってちょっとURLを触っております。
$exampleDetail = new Zend_Controller_Router_Route(
'example/:id/*',
array(
'controller' => 'example',
'action' => 'detail'
),
array('id' => '\d+')
);
$exampleInput = new Zend_Controller_Router_Route(
'example/input/:id/*',
array(
'controller' => 'example',
'action' => 'input'
),
array('id' => '\d+')
);
$router->addRoute('exampleDetail', $exampleDetail);
$router->addRoute('exampleInput', $exampleInput);
マリンピア神戸に久々に行きました。
うちの会社の東京で働いている人が丁度盆休みという事で
垂水に帰ってきてたので、大阪の先輩と元同僚を少数ですが集めて
垂水のマリンピア神戸に行ってきました。
久しぶりにアウトレットに行って驚いたのが、マリンピア神戸に立体駐車場ができている!
これには大阪の先輩も驚きを隠せなかった様子★
で、買った物は、カバン、先のブログでカメラを買ったと書かせてもらったのだが
一眼レフを入れるカバンが家には無く、それを入れる様に買いました。
BEAMSのアウトレットなので、安かった☆
デジタル一眼レフを買った。
名台詞管理Webサービス作ってみた。
漫画やアニメの名台詞を投稿していくWebサービス。
作ってみました。
適当に作ったわけじゃないですよ?(半分そうかも・・・)
名台詞って検索するとブログとか、個々のWebサイトだとか、掲示板だとか・・・
しかも「〜の名言集や、〜の名台詞集」等、既にテーマが決まっている事が前提での
名台詞集サイトがあって、名台詞が全て集まったサイトが見当たらなかった様に思えるので・・・
・・・作ってみました!!
盛り上がってくれれば嬉しいです。
[サービス内容]
登録されている作品毎に名台詞を閲覧、登録、管理を行うことができるサービスです。
名台詞の閲覧は誰でもできます。会員になると名台詞や作品の登録、管理を行うことができます。
自分が登録した台詞も表示でき、自分の好きな名台詞が管理できます。
また、他の会員の方が登録した名台詞一覧も可能です。
[ターゲット]
サイトの最初にも記載しているのですが
ターゲットは漫画やアニメが大好きなマニアック層です。
「最初は色々な分野に幅を広げた方がいいんじゃない?」という方もいらっしゃいます。
確かに入口が広い方が良いかもしれませんが、そうなるとサイト自身の方向性が
あらぬ方向へ向かってしまいそうで・・・怖い感じがしましたので
今のところは漫画やアニメ好きなマニアック層をターゲットとしております。
今後、幅を広げる事ができればと考えてます。
[今後]
登録されている名台詞を閲覧して、「あーその台詞!名台詞だねー」という
作品内で何気ない会話の台詞でも、ある人にとってはだたの台詞でも
ある人にとっては名台詞となっている人もいるかと思います。
その際に、「その台詞を名台詞と思っている人が居るとは!」というので「名台詞に共感」という
コメント機能を付ければ、また、コミュニケーションが発生するのではないかと考えております。
後、理想としては、名台詞の検索といえば「名台詞」「mzrf.org」という様に一般的に
世間一般に浸透していけば良いかなーと考えております。
Zend frameworkと入力関連の話
最近よくZend frameworkを使うことが増えました。
まぁよくある入力画面→確認画面→完了というのが日本の一般的な流れ♪
でも入力値があまりよろしくない場合は確認画面は表示せず
入力画面に戻したい!
でもZend frameworkのAction名の兼ね合いがあって
なんか意味分からなくなってくる!
という事でちょっと記載しておきま♪
多分このやり方が無難なのかな?とか思いながら。
ユーザ登録フォームがあるとする。
UserController.php
/**
@author Sackam
@copyright sackam.com
*/
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Session.php';
class UserController extends Zend_Controller_Action{
private $_sess;
private $_messages = array();
private $_req;
public function init(){
$this->_sess = new Zend_Session_Namespace('sample');
$this->_req = $this->getRequest();
$this->view->title = 'Zend framework sample form.';
}
/**
indexにアクセスがあった場合入力画面へリダイレクト
*/
public function indexAction(){
$this->_redirect($this->_req->getControllerName().'/input');
}
/**
入力画面用アクション
*/
public function inputAction(){
$this->view->methodTitle = '入力画面';
}
/**
確認画面アクション
入力エラーがある際は入力画面へ転送
*/
public function confirmAction(){
$this->view->methodTitle = '確認画面';
if($this->_req->isPost()){
$user['user_name'] = $this->_req->getPost('user_name');
$user['comment'] = $this->_req->getPost('comment');
$this->view->user = $user;
if($this->isDataValidate($user)){
$this->view->messages = $this->_messages;
$this->_forward('input');
}
}
}
/**
完了画面アクション
入力エラーがある際は入力画面へ転送、何なら不正アクセスとして扱っても良い。
*/
public function completeAction(){
$this->view->methodTitle = '完了画面';
if($this->_req->isPost() && $this->_sess->id != session_id()){
$user['user_name'] = $this->_req->getPost('user_name');
$user['comment'] = $this->_req->getPost('comment');
$this->view->user = $user;
if($this->isDataValidate($user) ||
$this->_req->getPost('back') != ''){
$this->_forward('input');
}else{
$this->_sess->id = session_id();
//データベース登録処理
}
}else{
$this->_redirect($this->_req->getControllerName().'/input');
}
}
/**
入力チェック 本来であればValidatorを使うのが一般的なのかな。
@param array $checkDatas
@return boolean
*/
private function isDataValidate(array $checkDatas){
$errorFlag = false;
if($checkDatas['user_name'] == ''){
$this->_messages['userNameError'] = 'ユーザ名は必須項目です。';
}
if($checkDatas['comment'] == ''){
$this->_messages['commentErorr'] = 'コメントは必須項目です。';
}
if(count($this->_messages) > 0){
$errorFlag = true;
}
return $errorFlag;
}
}
input.phtml
<html>
<head>
<title><?php echo $this->title?></title>
</head>
<body>
<h1><?php echo $this->methodTitle?></h1>
<?php if(count($this->messages) > 0){?>
<ul style="color:#FF0000">
<?php foreach($this->messages as $key => $value){?>
<li><?php echo $value?></li>
<?php }?>
</ul>
<?php }?>
<form name="form1" action="/labs/sample/user/confirm" method="post">
<p>
ユーザ名:<br />
<?php echo $this->formText('user_name', $this->user['user_name'])?>
</p>
<p>
コメント:<br />
<?php echo $this->formText('comment', $this->user['comment'])?></p>
<p>
<input type="submit" name="confirm" value="確認" />
</p>
</form>
</body>
</html>
confirm.phtml
<html>
<head>
<title><?php echo $this->title?></title>
</head>
<body>
<h1><?php echo $this->methodTitle?></h1>
<form name="form1" action="/labs/sample/user/complete" method="post">
<p>
ユーザ名:<br />
<?php echo $this->escape($this->user['user_name']) ?>
</p>
<p>
コメント:<br />
<?php echo $this->escape($this->user['comment']) ?>
</p>
<p>
<?php echo $this->formHidden('user_name', $this->user['user_name'])?>
<?php echo $this->formHidden('comment', $this->user['comment'])?>
<input type="submit" name="regist" value="登録" />
<input type="submit" name="back" value="入力画面に戻る" />
</p>
</form>
</body>
</html>
complete.phtml
<html> <head> <title><?php echo $this->title?></title> </head> <body> <h1><?php echo $this->methodTitle?></h1> <p> ここが完了画面です★<br />こんな感じでZend Frameworkとお付き合いすればいい感じっす。 </p> <p> <a href="/">こちらのリンクでトップへ戻ります。</a>的な感じの<br />リンクを付ければ登録できたっぽくフォームを作れそう★ </p> </body> </html>
実はinputActionではホボ何もせず
confirmActionとcompleteActionはほとんど一緒の事をしている。
ただcompleteActionではF5キーを押された事を防止する為セッション関連を使ってる★
まぁとりあえずこんな感じ。
これが結構シンプルなやり方かも♪
さて、ビバリーヒルズコップでも見ようかな。
しょうもない関数作ってみた
コピーライトの西暦を表示する関数・・・
function getCopy($startYear = null){
if($startYear == null){
return date('Y');
}else if($startYear == date('Y')){
return $startYear;
}else{
return $startYear . '-' . date('Y');
}
}
最近忙しい!!
忙しくでAIRやFlexが触れない!!
くそー土曜日こそ頑張ろ!






