2012年05月10日
講義061日目
------------------------------1H------------------------------
★symfony
●返信の作成
・default/templates/indexSuccess.php
のaタグで。commentテーブルのIDを渡すようにする
<br>
<a href="/reply/index/id/<?php echo $comment->getId()?>">返信する</a>
</div>
reply/actions/action.class.php
でIDを受け取る
------------------------------2H------------------------------
★symfony
●エラーチェック
・作りたいエラーチェックはvalidateフォルダにaction名.ymlファイルに記述する
required:
内容が入ってるかチェック
msg:
内容が無かった場合に表示したいメッセージ
・validate下のエラーチェックの内容を受けて動く処理
エラーチェックした後の処理をacction.class.phpに書く
public function handleErrorConfirm()
{
//confrimアクション宛てに送られたリクエストを受け取って
//indexアクションで引き継げるようにする
$this->request->setParameter("id",$this->getRequestParameter("comment_id"));
$this->request->setParameter("nickname",$this->getRequestParameter("nickname"));
$this->request->setParameter("content",$this->getRequestParameter("content"));
$this->forward("reply","index");
}
------------------------------3H------------------------------
★symfony
●executeConfirm()
public function executeConfirm()
{
$this->content = $this->getRequestParameter("content");
$this->nickname = $this->getRequestParameter("nickname");
$this->comment_id = $this->getRequestParameter("comment_id");
return sfView::SUCCESS;
}
●ヘルパー機能
・バリデーションのヘルパー機能を使う
<?php use_helper("Validation")?>
・メッセージを表示するform_error()
<?php echo form_error("nickname")?>
・エラーメッセージの矢印を編集
# validation_error_prefix: ' ↓ '
# validation_error_suffix: ' ↓'
↓書き換える
validation_error_prefix: ''
validation_error_suffix: ''
表示されなくなった
------------------------------4H------------------------------
★symfony
●エラーチェック
・多重投稿のエラーチェック
セッションを使う関数$this->setflush();
ユニークなキーをセッションに作成
・confirmSuccess.phpに追加セッション内容をhiddenでresultに渡す
<input type="hidden" name="reply_result_check" value="<?php echo $sf_flash->get("reply_result_check")?>">
executeResult()
public function executeResult()
{
if($this->getflash("reply_result_check") == $this->getRequestParameter("reply_result_check"))
{
$content = $this->getRequestParameter("content");
$nickname = $this->getRequestParameter("nickname");
$comment_id = $this->getRequestParameter("comment_id");
//新たなデータでreplyテーブルにレコードを挿入する
$reply = new Reply;
$reply->setContent($content);
$reply->setNickname($nickname);
$reply->setCommentId($comment_id);
$reply->save();
//指定したアクションにリダイレクトする
//$this->forward("default","index");
}
//URLごと遷移
$this->redirect("/");
}
//validate下のエラーチェックの内容を受けて動く処理
------------------------------5H------------------------------
★symfony
●ヘルパー
・linkヘルパー
layout.yml
<div id="header">
さんこんにちは
| <a href="/logout/">ログアウト</a>
| <a href="/backend/">管理者用ページへ</a>
</div>
↓
<div id="header">
<?php echo link_to("トップへ","/")?>
|
<?php echo link_to("管理者用ページへ","/backend/")?>
</div>
リロードするとURLに/index.phpが付いてしまうので、
setting.ymlのdevのno_script_name:をonにする
・form_tag
<form action="/bbssubmit/" method="post" enctype="multipart/form-data">
↓
<?php echo form_tag("/bbssubmit/","method=post multipart=ture")?>
・routhing.ymlの項目名に従わせる
ヘルパーのURL引数に「@~~」と書くとrouting.ymlの項目と連動
<?php echo form_tag("/bbssubmit/","method=post multipart=ture")?>
↓
<?php echo form_tag("@bbssubmit","method=post multipart=ture")?>
・input_tag
<input type="text" name="nickname" size="20"><br>
↓
<?php echo input_tag("nickname","",array("size" => 20)) ?>
第二引数にはデフォルトで入れておきたい文字列などを書いておくなどできる
・urlもinput_tagで書き換えてみる
<input type="text" name="url" size="60"><br>
↓
<?php echo input_tag("url","",array("size" => 60))?><br>
・texterea_tag
<textarea name="content" cols="50" rows="10"></textarea><br>
↓
<?php echo textarea_tag("content","",array("rows"=>10, "cols"=>55))?>
・select_tag
<select name="category_id">
<?php foreach($categorys as $k => $v):?>
<option value="<?php echo $k?>"><?php echo $v?></option>
<?php endforeach;?>
</select>
↓
<?php echo select_tag("category_id", options_for_select($categorys))?><br>
oputions_for_selectで指定した配列を元にセレクトボックスを作る
oputions_for_select()
の第二引数キー値を与えると、その要素が最初に選択される
<?php echo select_tag("category_id", options_for_select($categorys, 2))?><br>
・submit_tag
<input type="submit" value="投稿する">
↓
<?php echo submit_tag("投稿する")?>
●nl2br
/default/templates/indexSuccess.phpの投稿と返信のcontent
/reply/templates/indexSuccess.php
/reply/temptates/confirmSuccess.php
------------------------------6H------------------------------
★symfony
●画像のアップロードのヘルパー
・画像の保存するフォルダを作成
/web/uploads/tmp
/web/images/content
・eclipseでsshターミナルを右クリック
chmod 777 web/uploads/tmp
chmod 777 web/images/content
・input_file_tag
<?php echo input_file_tag("photo")?><br>
・executeSubmit()
ファイルを受け取る
$photo = $this->getRequest()->getFile("photo");
ファイルをDBに反映させる
if($photo["tmp_name"])
{
$filename = $comment->getId().".jpg";
$this->getRequest()->moveFile("photo",sfConfig::get('sf_web_dir')."/images/comment/".$filename);
$comment->setPhoto($filename);
$comment->save();
}
return sfView::SUCCESS;
}
-----------------------------memo------------------------------
0 件のコメント:
コメントを投稿