モノノフ日記

普通の日記です

symfony1.2でaskeet (Day3)

askeet day3は「symfonyMVCアーキテクチャ」の回です。日本語訳もあります。
今回からgitを導入して、githubでコード見れるようにしました。日ごとにbranch切っていくので見やすいかと。
404 · GitHub

全体レイアウトを変更

layout.php
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 
<?php echo include_http_metas() ?>
<?php echo include_metas() ?>
 
<?php echo include_title() ?>
 
<link rel="shortcut icon" href="/favicon.ico" />
 
</head>
<body>
 
  <div id="header">
    <ul>
      <li><?php echo link_to('about', '@homepage') ?></li>
    </ul>
    <h1><?php echo link_to(image_tag('askeet_logo.gif', 'alt=askeet'), '@homepage') ?></h1>
  </div>
 
  <div id="content">
    <div id="content_main">
      <?php echo $sf_content ?>
      <div class="verticalalign"></div>
    </div>
 
    <div id="content_bar">
      <!-- Nothing for the moment -->
      <div class="verticalalign"></div>
    </div>
  </div>
 
</body>
</html>

cssを公式リポジトリから取得して読み込むように設定

ちゃんと日ごとにタグ付けられてますが毎回アップデートするの面倒なのでいきなりtrunkから持ってきてます。

svn export http://svn.askeet.com/trunk/web/css/
cp ./css/*.css [askeet_project]/web/css/
apps/frontend/config/view.yml
-  stylesheets:    []
+  stylesheets:    [main, layout]

ルーティングのhomepageを再定義

homepage:
  url:    /
  param:  { module: question, action: index }

テストデータの作成

Chapter 16 - Application Management Tools (1_2) - Symfonyあたりを読むと、yamlファイルを分けて書いてもよさそうなのでModelごとに切り分けてテストデータを作成。

data/fixtures/100_user.yml
User:
  anonymouse:
    nickname:     anonymouse
    first_name:   Anonymouse
    last_name:    Coward
  fabien:
    nickname:     fabpot
    first_name:   Fabien
    last_name:    Potencier
  francois:
    nickname:     francoisz
    first_name:   Francois
    last_name:    Zaninotto
data/fixtures/200_question.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. 

  q2:
    title:      What can I offer to my step mother?
    user_id:    anonymouse
    body: |
      My stepmother has everything a stepmother is usually offered
      (watch, vacuum cleaner, earrings, del.icio.us account).
      Her birthday comes next week, I am broke, and I know that
      if I don't offer her something sweet, my girlfriend
      won't look at me in the eyes for another month. 

  q3:
    title:      How can I generate traffic to my blog?
    user_id:    francois
    body: | 
      I have a very swell blog that talks
      about my class and metas and pets and favorite movies.
data/fixtures/300_interest.yml
Interest:
  i1: { user_id: fabien, question_id: q1 }
  i2: { user_id: francois, question_id: q1 }
  i3: { user_id: francois, question_id: q2 }
  i4: { user_id: fabien, question_id: q2 }

サイトだとバッチファイルを作成してますが、CLIから一撃なのでそちらで。データを上書きしたくないときは --appendオプションつけると吉です。

symfony propel:data-load frontend --dir="data/fixtures"

questionモジュールのテンプレート編集

前回CRUDコマンドで作成したテンプレートを編集します。

indexSuccess.php
<?php use_helper('Text') ?>
 
<h1>popular questions</h1> 
 
<?php foreach($questions as $question): ?>
  <div class="question">
    <div class="interested_block">
      <div class="interested_mark" id="mark_<?php echo $question->getId() ?>">
        <?php echo count($question->getInterests()) ?>
      </div>
    </div>
 
    <h2><?php echo link_to($question->getTitle(), 'question/show?id='.$question->getId()) ?></h2>
 
    <div class="question_body">
      <?php echo truncate_text($question->getBody(), 200) ?>
    </div>
  </div>
<?php endforeach; ?>

画像ファイルを公式リポジトリから拝借します。

svn export http://svn.askeet.com/trunk/web/images/ 
cp ./images/* [askeet_project]/web/images/

本日の結果

f:id:Kiske:20081121151625p:image