2012年5月21日月曜日

2012年05月21日 講義068日目

2012年05月21日
講義068日目
------------------------------1H------------------------------
★smartyの復習
●VMwareでsmartyを使う
・smarty用のディレクトリをつくるなど
[nishi@localhost ~]$ cd sm.test.com/
[nishi@localhost sm.test.com]$ su -
パスワード:
[root@localhost ~]# vi /etc/httpd/conf.d/httpd-vhost.conf
<VirtualHost *:80>
    ServerName sm.test.com
    DocumentRoot /home/nishi/sm.test.com
    <Directory "/home/nishi/sm.test.com">
        AllowOverride All
        Options -Includes -ExecCGI
    </Directory>

[root@localhost ~]# /etc/init.d/httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中:                                            [  OK  ]   

・hostsの編集
SwichHostsでVMwareにつながるように設定
・smartyのインストール
libsの設置
・Smartyを使う
templates
templates_c
・chmod 777
[root@localhost sm.test.com]# ls -l
合計 16
-rw-rw-r-- 1 nishi nishi   19  5月 21 10:26 index.php
drwxrwxr-x 4 nishi nishi 4096  5月 21 10:32 libs
drwxrwxr-x 2 nishi nishi 4096  5月 21 10:34 templates
drwxrwxr-x 2 nishi nishi 4096  5月 21 10:35 templates_c
[root@localhost sm.test.com]# chmod 777 templates
[root@localhost sm.test.com]# chmod 777 templates_c
[root@localhost sm.test.com]# ls -l
合計 16
-rw-rw-r-- 1 nishi nishi   19  5月 21 10:26 index.php
drwxrwxr-x 4 nishi nishi 4096  5月 21 10:32 libs
drwxrwxrwx 2 nishi nishi 4096  5月 21 10:34 templates
drwxrwxrwx 2 nishi nishi 4096  5月 21 10:35 templates_c
・displayでテンプレートを呼び出す
index.tpl
<div id="contentWrap">
<h1>テスト</h1>
</div>
------------------------------2H------------------------------
★smartyの復習

・index.php
<? php
require_once("./libs/Smarty.class.php");
$sm = new Smarty;
$sm->display("index.tpl");

・assign
phpとtplで情報の受け渡し
index.tpl
{$categorys.1}

index.php
$sm->assign("categorys", array("勉強","遊び","その他"));

・foreach
ループさせる
{foreach from=$categorys item=category key=k}
カテゴリー:{$category}<br>
{/foreach}

・{* コメント*}
コメント

・include
{include file="header.tpl"}
他の.tplファイルを読み込む

・fetch
ロジック次第で切り替えるときに使用
index.php
$sitemenu = $sm->fetch("sitemenu.tpl");
$sm->assign("sitemenu",$sitemenu);

index.tpl
<div id="sitemenu">
{$sitemenu}
</div>

sitemenu.tpl
<ul>サイトメニュー
{foreach from=$menuarr item=menu key=k}
  <li><a href="{$k}">{$menu}</a></li>
{/foreach}
</ul>

・直接テンプレートでセッションを使うとき
index.php
$_SESSION["nowtime"] = date();

index.tpl
現在の年月日時:{$smarty.session.nowtime}

・getとか使う場合
GETで渡ってきた値:{$smarty.get.id1}と{$smarty.get.id2}
<form action="/" method="get">
<input type="text" name="id1">
<input type="text" name="id2">
<input type="submit" value="送信">
</form>

・|htmlspecialcharsなどの関数を使う
{$smarty.get.id2|htmlspecialchars}

●smartyとは関係ないおまけ
・関数のスコープの外側でも参照できる定数を作る
$_SESSIONとかが定数

例)関数を作る
$_SESSION["nowtime"] = getNowDate();

function getNowDate(){
    $date = date("Y-m-d H:i:s");
    return $date;
}

・定数を定義する
//定数を定義するための関数
define(NOW_TIME, date("Y-m-d H:i:s"));

作られた定数を使う
function getNowDate(){
    $date = NOW_TIME;
    return $date;
}

・コーディング規約
用意されてる定数と違い、頭に$は付けない
大文字と_のみで作る
別ファイルでは使えないので、定数を設定したファイルを用意して読み込むのがいい
------------------------------3H------------------------------
★smartyテスト
------------------------------4H------------------------------
★symfony
●url直打ち対策
executeIndex()
        //URL直打ち対処
        $this->bbs_submit_check = rand();
        $this->setflash("bbs_submit_check",$this->bbs_submit_check);
executeSubmit()
        //URL直打ち対処
        if($this->getRequestParameter("bbs_submit_check") !=
           $this->getflash("bbs_submit_check") ||
           $this->getRequestParameter("bbs_submit_check") == ""
           )
        {
            $this->redirect("/");
        }
        $this->setflash("bbs_submit_check","");
indexSuccess

・replayにも直打ち対策を入れる

        if($this->getflash("reply_result_check") == $this->getRequestParameter("reply_result_check")
        && $this->getRequestParameter("reply_result_check") != "")
        {
         省略
        }

セッションをクリアする
$this->setflash("reply_result_check","");

sf.shonanbbs.com/reply/result/comment_id/89などで直打ちしてトップにリダイレクトする

------------------------------5H------------------------------
★symfony
●url直打ち対策
・executeConfrm()

        $hostname = sfConfig::get("sf_hostname");
        $ref = $this->getRequest()->getReferer();
        if($ref != $hostname."reply/confirm" && srtpos($ref, $hostname,"reply/index"!== false))
        {
            $this->redirect("/");
        }

sf.shonanbbs.com/reply/confirmを直打ちしてテスト

●プラグイン
・sfPageFlowのインストール
symfonyのCRUD機能を強化してくれる
https://github.com/tumf/Symfony1.0_sfPageFlowPlugin
フォルダ名が長いのでsfPageFlowPluginにリネーム
/pulginにコピー
・自動生成タスクの設置
data/tasks
  コマンド対処
  /dataに/tasksを設置
アドオン
  symfony側でのロジック
  symfonyをインストールした場所に設置
スケルトン
  作成される雛形
  symfonyをインストールした場所に設置
 
[root@localhost nishi]# cp sfPropel* /usr/share/pear/symfony/addon/propel/generator
[root@localhost nishi]# cp -rf generator/sf* /usr/share/pear/data/symfony/generator
上書きは許可
環境によってアドオンとスケルトンを置く場所は異なるので調べてから置く
symfony propel-generate-create backend member_add Member

------------------------------6H------------------------------
★symfony
●プラグイン
・data/tasks
#CREATE
symfony propel-generate-create アプリケーション名 モジュール名 モデル名
#EDIT
symfony propel-generate-edit アプリケーション名 モジュール名 モデル名
#LIST
symfony propel-generate-list アプリケーション名 モジュール名 モデル名
が使えるようになった
・プラグインの消去
ファイルを消去すればいい
・pageflow.ymlで設定を編集する
Processはactionのこと
Displayはtemplateのこと

firstState: ProcessInitialize←ここに最初にたどり着く
lastState:  DisplayResult←最後にここにたどり着く
・新規追加のリンクを有効にする
実際のurlと飛ばす先が違うのでルーティングで制御
routhing.ymlに
member_add
  url:   /member/add
  param: { module: member_add, action: initialize }
を追加
-----------------------------memo------------------------------

0 件のコメント:

コメントを投稿