marunomaruno-memo

marunomaruno-memo

symfony への MVC アーキテクチャーの適用

2008年12月28日 | PHP
symfony への MVC アーキテクチャーの適用
================================================================================

symfony で、MVC モデルの M (Model) も使ったサンプル。

ここでは、BMI 値の算出を symfony で作る。

画面の流れは、以下のとおり。

(1) ブラウザーで URL 入力
(2) BMI 算出のための身長と体重を入力する画面表示
(3) 入力値を送信
(4) BMI 値の画面表示


■bmi アプリケーションとモジュールの作成

□bmi アプリケーションの作成

---
C:\xampp\htdocs\symfony>symfony init-app bmi
PHP Warning: Zend Debug Server for PHP 5.2.x cannot be found (expected at 'C:\x
ampp\php\zendOptimizer\lib\Debugger\php-5.2.x\ZendDebugger.dll') - try reinstall
ing the Zend Debug Server in Unknown online 0
>> dir+ C:\xampp\htdocs\symfony\apps/bmi\config
:
>> chmod 666 C:/xampp/htdocs/symfony/log/hello_dev.log
>> dir+ C:\xampp\htdocs\symfony\test/functional/bmi
---

□bmi モジュールの作成

---
C:\xampp\htdocs\symfony>symfony init-module bmi bmi
PHP Warning: Zend Debug Server for PHP 5.2.x cannot be found (expected at 'C:\x
ampp\php\zendOptimizer\lib\Debugger\php-5.2.x\ZendDebugger.dll') - try reinstall
ing the Zend Debug Server in Unknown online 0
>> dir+ C:\xampp\htdocs\symfony\apps\bmi\modules/bmi\actions
:
>> tokens C:/xampp/htdocs/symfony/apps/bm.../bmi/templates/indexSuccess.php
---


■アクション(コントロール)の作成

ブラウザーからの最初のリクエストを処理する executeIndex() メソッドと、
身長と体重を受け取り、BMI のオブジェクトを生成して、表示用のテンプレートに渡す e
xecuteShow() メソッドの 2 つを作ります。

modules/sbmi/actions/actions.class.php
------------------------------------------------------
<?php
require_once("BMI.class.php");
?>
<?php
/**
 * sbmi actions.
 *
 * @package    symfony
 * @subpackage sbmi
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class sbmiActions extends sfActions
{
    public function executeIndex()
    {
        //    $this->forward('default', 'module');
    }

    public function executeShow()
    {
        $request = $this->getRequest();

        $this->bmi = new BMI();                                     // (1)
        $this->bmi->setHeight($request->getParameter("height"));    // (2)
        $this->bmi->setWeight($request->getParameter("weight"));    // (2)
        //    $this->forward('default', 'module');
    }
}
?>
------------------------------------------------------



□ $this->bmi = new BMI(); // (1)

モデルのオブジェクトを生成。

□ $this->bmi->setHeight($request->getParameter("height")); // (2)
$this->bmi->setWeight($request->getParameter("weight")); // (2)

モデルに対してデータを設定。


■モデルの作成

BMIクラス。

今回は、modules/sbmi/actions に作る。

modules/bmi/actions/actions.class.php
------------------------------------------------------
<?php
/**
 * 身長と体重をもとにそのBMIを管理する
 * @author marunomaruno
 * @version 3.1 symfony 版
 *
 */
class BMI {
    private $height;
    private $weight;

    /**
     * 身長を設定する
     * @param $height 身長(cm)
     */
    public function setHeight($height) {
        $this->height = $height;
    }

    /**
     * 身長を取得する。
     * @return 身長(cm)
     */
    public function getHeight() {
        return $this->height;
    }

    /**
     * 体重を設定する
     * @param $height 体重(kg)
     */
    public function setWeight($weight) {
        $this->weight = $weight;
    }

    /**
     * 体重を取得する。
     * @return 体重(kg)
     */
    public function getWeight() {
        return $this->weight;
    }

    /**
     * BMIを取得する。
     * @return BMI
     */
    public function getBmi() {
        return $this->weight / pow($this->height / 100, 2);
    }
}
------------------------------------------------------



■ビューの作成

executeIndex() メソッドからフォーワードされてくるのは、indexSuccess.php ファイル。
また、executeShow() メソッドからフォーワードされてくるのは、showSuccess.php ファ
イル。

これらはネーミングで紐付けされる。

アクション名 アクションクラスのメソッド名 テンプレートのファイル名
------------ ---------------------------- ------------------------
xxxx executeXxxx() xxxxSuccess.php


□入力フォームを表示するビュー(テンプレート)の作成

modules/sbmi/templates/indexSuccess.php
------------------------------------------------------
<?php use_helper('Form'); ?> 

<h1>BMI</h1>

<?php echo form_tag("bmi/show", array("method"=>"post")); ?>
<br>
身長 <?php echo input_tag("height", ""); ?> cm

<br>
体重 <?php echo input_tag("weight", ""); ?> kg

<br>
<?php echo submit_tag("送信"); ?>

</form>
------------------------------------------------------



□BMI の結果を表示するビュー(テンプレート)の作成

modules/bmi/templates/showSuccess.php
------------------------------------------------------
<h1>BMI</h1>
あなたの BMI 値は、<?php echo $bmi->getBmi(); ?> です。
------------------------------------------------------



以上


最新の画像もっと見る

コメントを投稿