2012年5月14日月曜日

2012年05月14日 講義063日目

2012年05月14日
講義063日目
------------------------------1H------------------------------
★symfony
●ログアウトモジュールの作成
・backendのyaloutにlogoutへのリンクを作成
<?php echo link_to("ログアウト","/logout")?>
・setting.yml
no_script_nameをon
・コマンドでモジュールを作成
[nishi@localhost ~]$ cd sf.shonanbbs.com/
[nishi@localhost sf.shonanbbs.com]$ symfony init-mod backend logout
・/apps/backend/modules/actions/logout.php
        //認証済み解除
        $this->getUser()->setAuthenticated(false);
        //権限のクリア
        $this->getUser()->clearCredentials();
        //sessionの開放
        $this->getUser()->getAuttributeHolder()->removeNamespace(sfConfig::get('sf_session_name'));

        //ホームページに戻る
        return $this->redirect('/');
        //$this->forward('default', 'module');
・seting.ymlの変更
#all
のコメントアウトをはずす


------------------------------2H------------------------------
★symefony
●ログインに飛ばす処理をbackendに作る
・backend/modules/actions/action.class.php
    public function executeLogin()
    {
        $this->redirect("http://sf.shonanbbs.com/login");
    }
・front/modules/actions/action.class.php
バリデーション(エラーチェック)
<?php use_helper("Validation")?>

<?php echo form_tag('login/index')?>
↑で指定したactionにメソッドを書く
・apps/front/modules/validate/index.ymlの作成
fields:
  mailaddress:
    required:
      msg: メールアドレスを入力してください。
    sfEmailValidator:
      sfrict:        true
      email_error: このメールアドレスは無効です

  password:
    required:
      msg: パスワードを入力してください。
    sfStringValidator:
      min:          2
      min_error:    少なくとも2文字入力してください。
      max:          20
      max_error:    20文字以下で入力してください。
------------------------------3H------------------------------
★symefony
●バリデーション
・/apps/front/mudules/actions/action.class.php
executeIndex()をexecuteSubmit()に変更
    public function executeSubmit()
    {
        $this->mailaddress = $this->getRequestParameter('mailaddress');
        $this->password = $this->getRequestParameter('password');

        if($this->mailaddress !='' && $this->password != '')
        {
            //メールアドレスとパスワードを元にmemberテーブルのインスタンス取得
            $c = new Criteria;
            $c->add(MemberPeer::MAILADDRESS, $this->mailaddress);
            $c->add(MemberPeer::PASSWORD, $this->password);
            $member = MemberPeer::doSelectOne($c);
            //$member = MemberPeer::retrieveByPassword($this->mailaddress);

            if($member)
            {
                //ログイン状態にする
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->clearCredentials();
                $this->getUser()->addCredential('member');

                //会員情報をセッションにセット
                $this->getUser()->setAttribute('member_id',$member->getId(),sfConfig::get('sf_session_name'));
                $this->getUser()->setAttribute('nickname', $member->getNickname(), sfConfig::get('sf_session_name'));
                $this->redirect('/backend');
            }

        }
        $this->redirect("/");
        //$this->forward('default', 'module');
    }
・新しくexecuteIndex()を作成
    public function executeIndex()
    {
        $this->mailaddress = $this->getRequestParameter('mailaddress');
        return sfView::SUCCESS;
・indexSuccessをエラーメッセージ表示箇所を変更
・パスワード部分も変更
<?php echo input_password_tag('password', "", array('size' => 50));?>
第二引数を""にして表示されないようにする
・/apps/front/mudules/actions/action.class.php
    //バリデーションに引っかかったら動く処理
    public function handleErrorSubmit()
    {
        $this->request->setParameter("mailaddress", $this->getRequestParameter('mailaddress'));
        $this->forward("login", "index");

    }
------------------------------4H------------------------------
★symfony
●カスタムバリデーター
・submit.yml
myLoginCheckValidator:の作成
mailaddress:内に
   myLoginCheckValidator:
     login_error: 一致するユーザがいませんでした。
を書き加える
・/lib/model/validatorフォルダの作成
・myLoginCheckValidator.class.php
<?php
class myLoginCheckValidator extends sfValidator
{
    public function execute(&$value, &$error)
    //$value,$errorはフォームから受け取ったデータ
    {
        //バリデート内での他のリクエストやユーザ情報の取得方法
        $request = sfContext::getInstance()->getRequest();
        $user = sfContext::getInstance()->getUser();

        $mailaddress = $request->getParameter("mailaddress");
        $password = $request->getParameter("password");

        $c = new Criteria;
        $c->add(MemberPeer::MAILADDRESS, $mailaddress);
        $c->add(MemberPeer::PASSWORD, $password);
        $member = MemberPeer::doSelectOne($c);

        //メールアドレスとパスワードが一致するレコードが無いなら
        if(!$member)
        {
            $error = this->getParameter("login_error");
            return false;
        }
        return true;

    }
}
?>
------------------------------5H------------------------------
★symfony
●404エラーを吐いたときに/に飛ばす
・backned/modules/default//action.class.php
    public function executeError404()
    {
        $this->redirect("/");
    }
・RewriteRule ^(backend/.*)$ backend/backend.php [QSA,L]

・backend/config/setting.yml
#    error_404_module:       default   # To be called when a 404 error is raised
#    error_404_action:       error404  # Or when the requested URL doesn't match any route

    error_404_module:       default   # To be called when a 404 error is raised
    error_404_action:       error404  # Or when the requested URL doesn't match any route
・frontにも適用させる
front/default/actions/action.class.phpにも
    public function executeError404()
    {
        $this->redirect("/");
    }
    を作成
front/config/setting.ymlも同様に変更
●[nishi@localhost sf.shonanbbs.com]$ symfony propel-generate-crud backend comment Comment
Commentテーブルのcrudを行う、commentモジュールをbackendに作る
/apps/backend/modules/comment/actions/action.class.phpが作成される
・管理者画面のエスケープ
backend/config/setting.yml
#    escaping_strategy:      bc        # Determines how variables are made available to templates. Accepted values: bc, both, on, off. The value off deactivates escaping completely and gives a slight boost.
#    escaping_method:   ESC_ENTITIES   # Function or helper used for escaping. Accepted values: ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES.

    escaping_strategy:      both        # Determines how variables are made available to templates. Accepted values: bc, both, on, off. The value off deactivates escaping completely and gives a slight boost.
    escaping_method:   ESC_ENTITIES   # Function or helper used for escaping. Accepted values: ESC_RAW, ESC_ENTITIES, ESC_JS, ESC_JS_NO_ENTITIES.

・バリデーションエラー
#    validation_error_prefix:    ' &darr;&nbsp;'
#    validation_error_suffix:    ' &nbsp;&darr;'

    validation_error_prefix:    ''
    validation_error_suffix:    ''
------------------------------6H------------------------------
★symfony
●app.yml
/config/settings.php
all:
  .globals:
    categorys:
      1: 勉強について
      2: 遊びについて
      3: その他
をbackend/config/app.ymlにコピー
どちらからでも読み込めることを確認するためのテスト

●CRUDに際し、URL直打ちを回避する
/backend/comment/update
と直打ちしたら弾かれて管理画面のトップページへ飛ぶこと
http://sf,shonanbbs.com/backend/comment/create
http://sf,shonanbbs.com/backend/comment/edit/id/87
↑らのいずれかから、saveボタンを押して新規作成、更新ができること

    public function executeUpdate()
    {
        $create_url = sfConfig::get("sf_hostname")."backend/comment";
        $edit_url = sfConfig::get("sf_hostname")."backend/comment/edit/id";
        $ref = $this->getRequest()->getReferer();

        if(!(
        $ref == $create_url || strpos($ref, $edit_url) === 0))
        {
            $this->redirect("/");
        }
●部品化
・パーシャル
テンプレート単位の部品
・スロット
データ単位の部品
・コンポーネント
データベース、ロジック、テンプレート全てが入った部品
●メニューを部品化
・/backend/templates/_menu.phpを作成
/backend/templates/layout.phpの内容を_menu.phpに移動
・<div id="contentRight"></div>の中身を_menu.phpに移動
・layout.phpでincludeする
  <div id="contentRight">
<?php include_partial("global/menu")?>
  </div>
-----------------------------memo------------------------------

0 件のコメント:

コメントを投稿