symfony vol.13

【授業内容】
askeet 9日目

☆questionとanswerでリッチテキストを使えるようにする!

?? リッチテキストって…?
   文字の大きさや色、書式などの情報がタグを用いて文書中に盛り込まれたもの。

Markdownを使おう!

?? Markdown……ってなに?
[画像挿入予定地1]

これ自体はまだPCの中に入ってないから、リンクからDLする。
askeet/lib/以下にDLした"Markdown.php"ってファイルをおく。
↑これを呼んで使いたいときは、

  require-once("mark-down.php");

で呼び出せばOKだよ!

[画像挿入予定地2]

■ Modelを拡張する。

手順1、Questionテーブルに新しいColumn(html_body)を作ろう!

  askeet/config/schema.yml

○ 開いたら、Questionのところに、以下の一文を追加。

html_body: { type:longvarchar }


○ schema.ymlをいじったらいつもの三点セット

$symfony propel-build-model
$symfony propel-build-sql
$symfony propel-insert-sql
手順2、setBodyメソッドを書き換えよう!


QuestionクラスのsetBodyが呼ばれたとき、Markdownに従って、html_bodyを作って保存しないと×

  askeet/lib/model/Quesiton.php

○ 開いたら、以下のコードを追加する。

public function setBody($v)
{
  parent::setBody($v);
 
  require_once('markdown.php');
 
  // HTMLタグを剥ぎ取る
  $v = htmlentities($v, ENT_QUOTES, 'UTF-8');
 
  $this->setHtmlBody(markdown($v));
}

[画像挿入予定地2]

?? htmlentites……ってなに?
エンコードした文字列を返す
[画像挿入予定地3]

○ テストデータを更新する。
[画像挿入予定地4]

  askeet/data/fixtures/test_data.yml

○ 開いたら以下のデータを挿入

Question:
  q1:
    title: What shall I do tonight with my girlfriend?
    user_id: fabien
    body:  |
      We shall meet in front of the __Dunkin'Donuts__ before dinner, 
      and I haven't the slightest idea of what I can do with her. 
      She's not interested in _programming_, _space opera movies_ nor _insects_.
      She's kinda cute, so I __really__ need to find something 
      that will keep her to my side for another evening.

○ 書き換えたら、データベースに再投入!

symfony propel-load-data
手順3、templateを修正しよう!

[画像挿入予定地5]

  askeet/apps/frontend/modules/question/tenplates/showSuccess.php

○ 開いたら、

<?php echo $question -> getBody() ?>

を、

<?php echo truncate_text(strip_tags($question->getHtmlBody()), 200) ?>

に書き換える。

?? strip_tags……って、何?
・タグを除去した文字列を返す。

[画像挿入予定地6]


☆ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー☆

★ ここまでのおさらい(リッチテキストを使えるようにする)

1 テーブルにリッチテキスト用のカラムを1つ用意する。(schema.yml)

2 setBodyメソッドを書き換える

3 templateを修正する

☆ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー☆

[画像挿入予定地7]

!! その前に!!7日目でパーシャルにし損ねてた、answer/_list.phpを作っちゃおう!

[画像挿入予定地8]

[画像挿入予定地9]

……やっていることは同じなので、ここでは割愛……

☆ パラメータに出ちゃってるIDを隠そう!

[画像挿入予定地10]

手順1 actionを変更する(userモジュールのshowアクション)

  askeet/apps/frontend/modules/user/actions/action.class.php

○ 開いたら、executeShow()を以下の用に書き換える。

public function executeShow()
{
  $this->subscriber = UserPeer::retrieveByNickname($this->getRequestParameter('nickname'));
  $this->forward404Unless($this->subscriber);
 
  $this->interests = $this->subscriber->getInterestsJoinQuestion();
  $this->answers   = $this->subscriber->getAnswersJoinQuestion();
  $this->questions = $this->subscriber->getQuestions();
}
手順2 modelを変更する (UserPeerクラス)

[画像挿入予定地11]

○開いたら、以下のメソッドを追加する。

public static function retrieveByNickname($nickname)
{
  $c = new Criteria();
  $c->add(self::NICKNAME, $nickname);
 
  return self::doSelectOne($c);
}
手順3 templateを変更する (questionモジュールのshowSuccess.phpと_list.php)

  askeet/apps/frontend/modules/question/tenplates/showSuccess.php
  askeet/apps/frontend/modules/question/tenplates/_list.php

○ 開いたら、

<?php echo link_to($question->getUser(), 'user/show?id='.$question->getUserId()) ?>

のところを、

<?php echo link_to($question->getUser(), 'user/show?nickname='.$question->getUser()->getNickname()) ?>

に置き換える。

※ answerモジュールの_answer.phpにも同じ処理をする。

手順4 ルーティングルールを変更する (routing.yml)

[画像挿入予定地12]

  askeet/apps/frontend/config/routing.yml

○ 開いたら、新しいルールを追加する。

user_profile:
  url:   /user/:nickname
  param: { module: user, action: show }    
手順5 symfony cc して、URLがどうなってるかを確認してみよう!

☆ ルーティングを編集しよう!

[画像挿入予定地13]

  askeet/apps/frontend/config/routing.yml

○開いたら、以下をもりもりぃーっと編集。

# question
question:
  url:   /question/:stripped_title
  param: { module: question, action: show }

popular_questions:
  url:   /index/:page
  param: { module: question, action: list, page: 1 }

recent_questions:
  url:   /recent/:page
  param: { module: question, action: recent, page: 1 }

add_question:
  url:   /add_question
  param: { module: question, action: add }

# answer
recent_answers:
  url:   /recent/answers/:page
  param: { module: answer, action: recent, page: 1 }

# user
login:
  url:   /login
  param: { module: user, action: login }

logout:
  url:   /logout
  param: { module: user, action: logout }

user_profile:
  url:   /user/:nickname
  param: { module: user, action: show }

# default rules
homepage:
  url:   /
  param: { module: question, action: list }

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

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

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

[画像挿入予定地14]


☆★*******☆*******★*******★☆
9日目終了!お疲れさまでした!!
☆★*******☆*******★*******★☆



以上、今日の授業でした!