2012年5月9日水曜日

2012年05月09日 講義060日目

2012年05月09日
講義060日目
------------------------------1H------------------------------
★symfony
●エスケープの設定
/apps/front/config/settings.yml
・55,56行目を変更
#    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に変更
フォーム、画面出力の両方にエスケープをかける
bc
both
on
off
のパラメータがあるので必要に応じて設定を変更する
    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.
・21行目の#もコメントをはずす
●ymlに変更を加えたら、
symfony cc
を実行しておく
[nishi@localhost sf.shonanbbs.com]$ symfony cc
●css読み込み
方法は二つ
①frontのリンクを変える方法
②symfonyの設定ファイルで読込先を変える方法
・②のやり方
/apps/front/config/view.yml
☆ymlファイルはスペースにシビアなので注意
, (カンマ、スペース)で複数の設定を追加できる
default:
  http_metas:
    content-type: text/html

  metas:
    title:        symfony project
    robots:       index, follow
    description:  symfony project
    keywords:     symfony, project
    language:     en

  stylesheets:    [main]

  javascripts:    []

  has_layout:     on
  layout:         layout


default:
  http_metas:
    content-type: text/html

  metas:
    title:        symfony版湘南掲示板
    robots:       index, follow
    description:  湘南の掲示板です
    keywords:     湘南, 掲示板
    language:     ja

  stylesheets:    [style]

  javascripts:    []

  has_layout:     on
  layout:         layout
------------------------------2H------------------------------
★symfony

・cssを対応させる
・フォームを作る
------------------------------3H------------------------------
★symfony
●submitのactionを作成
・apps/front/modules/default/actions/action.class.php
    public function executeSubmit()
    {
        //GETまたはPOSTどちらでもgetRequestParameterで受け取れる
        $nickname = $this->getRequestParameter("nickname");
        $content = $this->getRequestParameter("content");
        $url = $this->getRequestParameter("url");
        exit;
        return sfView::SUCCESS;
    }
・受け取った新規データをDBのcommentテーブルに入れる
        //新規データをcommentテーブルに入れる
        $comment = new Comment;
        //新規データをセットしていく、カラム名でメソッドが作られている
        $comment->setNickname($nickname);
        $comment->setContent($content);
        $comment->setUrl($url);
        //「created_at」というカラムがあった場合、自動で現在時刻で
        //データを作成してくれる。

・データをセーブする
        //セットした値をデータベースに反省させる
        $comment->save();
●submitSuccess.phpの作成
・apps/front/module/templates/submitSuccess.php
投稿が完了しました<br>
<a href="/">戻る</a>
●投稿順をCriteriaの関数で変更
        $c = new Criteria;
        $c->addDescendingOrderByColumn(CommentPeer::CREATED_AT);
        //ascはaddAscendingOrderByColumn
        //ORDER BY created_at DESCのsql文と同じ
        $this->comments = CommentPeer::doSelect($c);
------------------------------4H------------------------------
★symfony
●.htaccess
・index.phpに飛ばす設定が書いてある
frontコントローラを作成したときに.htaccessが作成される
index.php内でindex.php/~~のパスのindex.php部分を省略できる
・setting.yml
no_script_name:をonにしてるとパスのindex.phpを省略可能
・モジュール名の変更
routing.yml
同一のアプリケーション下で、URLと実際のモジュール・アクションを紐付ける
# default rules
homepage:
  url:   /
  param: { module: default, action: index }

default_symfony:
  url:   /symfony/:action/*
  param: { module: default }

default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*

・routing.ymlに追記
/default/submit/を/bbssubmit/に名前を変更
bbssubmit:
  url:   /bbssubmit/
  param: { module: default, action: submit }
を追加
symfony ccで設定を反映させる
・/bbssubmitでも/bbssubmit/に飛ばしてほしい場合
追記する
bbssubmit_sub:
  url:   /bbssubmit
  param: { module: default, action: submit }
symfony ccで設定を反映させる
●キャメルケースのルール
カラム名にアンダーバーが入ってる場合の各処理での記述
・propelのmoduleのメソッド
キャメルケースになる
created_at → getCreatedAt
・Criteriaのメソッド内での記述
大文字+アンダーバーになる
created_at → CREATED_AT
------------------------------5H------------------------------
★symfony
●設定ファイルの作成
・設定ファイルの優先順位
モジュール>アプリケーション>プロジェクト
・/config/settings.ymlの作成
all:
  .globals
    categorys:
      1: 勉強について
      2: 遊びについて
      3: その他
・action.class.phpに追記
$this->categorys = sfConfig::get("sf_categorys");
・action.class.phpに追記
public function executeSubmit()に追記
受け取る処理
$category_id = $this->getRequestParameter("category_id");
DBに渡す処理
$comment->setCategoryId($category_id);
を追記
------------------------------6H------------------------------
★symfony
●/config/config.php
// symfony directories
$sf_symfony_lib_dir  = '/usr/share/pear/symfony';
$sf_symfony_data_dir = '/usr/share/pear/data/symfony';
↑symfonyがインストールされてる場所が記されている
●バリデーション
ymlファイルで設定できる
●定数
define関数
内部定数
●replyモジュールの作成
[nishi@localhost sf.shonanbbs.com]$ symfony init-mod front reply
-----------------------------memo------------------------------

0 件のコメント:

コメントを投稿