ushidayの日記

主に「IBMi」のメモに・・・

Zend Framework Controllerメモ

Zend_Controllerを使ってみた時のメモを残しておきます。

httpd.confの設定

Zend Framework(ZF)では、URL変換に”mod_rewrite”モジュールを使用しているので、「/usr/local/Zend/apache2/conf/httpd.conf」を以下の様に書き換えます。

#LoadModule rewrite_module modules/mod_rewrite.so
↓
LoadModule rewrite_module modules/mod_rewrite.so

ちなみに、”Zend Core 2.6.1”はデフォルトでモジュールが有効になってました。”.htaccess”を有効にする為に、””以下に追記します。

# ルートの設定
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

# ドキュメントルートの設定
<Directory "/www/zendcore/htdocs">
    Options Indexes FollowSymLinks

#    AllowOverride None
    ↓
    AllowOverride All

    Order deny,allow
        Deny from all
    Allow from 127.0.0.1
</Directory>
.htaccessの設定

Zend StudioZend Frameworkプロジェクトで作成すると以下の構造になり、コントローラ、ビューの雛形や.htaccessもデォルトで用意されますので、それを適宜変更します。

■デフォルトの.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Controllerを使う
    • フロントコントローラの準備。(e.g. PROJECT_HOME/public/index.php
    • Zend_Controller_Actionのサブクラスのアクションコントローラを準備。(e.g. PROJECT_HOME/application/controllers/IndexController.php
    • 継承されるメソッド
      • init : コントローラの初期化の実装をオーバーライド
      • preDispatch : Actionメソッド実行前のフックの実装をオーバーライド。
      • postDispatch : Actionメソッド実行後のフックの実装をオーバーライド。
      • _forward(アクション, クラス=null, モジュール, 引数) : アクション実行後にフォワード。preDispatchで行った場合は、アクションがキャンセルされる。(e.g. $this->_forward("hogehoge","hoge")<=> "/hoge/hogehoge" )
      • _redirect(URI) : URIへリダイレクトする。(e.g. $this->_redirect("/hoge/hogehoge"))
    • Viewを準備。(e.g. PROJECT_HOME/application/views/scripts/index/index.phtml)
    • 命名規則
      • コントローラクラス : 「HogeController.php」(MSD1が大文字)
      • アクションメソッド : 「hogehogeAction」(小文字名+Action)
      • ビュー : 「hogehoge.phtml」(小文字名)
    • URIにコントローラとメソッドを指定しない場合のデフォルト
      • アクションコントローラ : 「IndexController.php」(setDefaultControllerNameで変更可)
      • アクションメソッド : 「indexAction」(setDefaultActionで変更可)

■フロントコントローラの例

<?php
require_once 'Zend/Controller/Front.php';

/*
Zend_Controller_Front::run("../application/controllers");
↑は↓と同意
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory("../application/controllers");
$front->dispatch();
*/
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory("../application/controllers");

// 独自のデフォルトコントローラとデフォルトメソッドを指定する場合 
/*
$front->setDefaultControllerName("hoge");
$front->setDefaultAction("hogehoge");
*/

$front->dispatch();

■アクションコントローラの例

<?php

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Date.php';

class hogeController extends Zend_Controller_Action
{

    public function init()
    {
        /* 初期化処理をここに記述する。コンストラクタの後に実行される */
        $this->view->assign("message","初期化を行いました。");
        $this->view->assign("message3","forwardするとコントローラの初期化で消える");
    }

    public function preDispatch(){
    /* アクションの呼出前に実行する処理 */
    }

    public function postDispatch(){
        /* アクションの呼出後に実行する処理 */
    	$this->view->assign("message4","アクションを実行しました。");
    }
  
    public function indexAction()
    {
        $this->view->assign("actionname","indexアクションです。");
        $this->view->assign("message3","forwardするとこのメッセージは消される。");
        $date = new Zend_Date();
        $this->view->assign("dt",$date->get(Zend_Date::TIMES));

        if(($date->compare("09:00:00",Zend_Date::TIMES) < 0)
           || ($date->compare("17:00:00",Zend_Date::TIMES) > 0)){
            $this->view->assign("message2","9〜17時以外は稼働時間外です");
            // 条件によって、別のアクションにフォワードする例
            // forward(アクション名,コントローラ名,モジュール名,パラメータ)
            $this->_forward("hogehoge");
        }else{
            $this->view->assign("message2","システム稼働中");               
        }        
    }
    
    public function hogehogeAction()
    {
        $this->view->assign("actionname","hogehogeアクションです。");        
    }    
}

■ビューの例

<html>
<head>
<title>タイトル</title>
</head>
<body>
アクション名:「<?php echo $this->escape($this->actionname);?><br/>
現在の時刻は「<?php echo $this->escape($this->dt);?>」です。<br/>

<?php echo $this->escape($this->message);?><br/>
<?php echo $this->escape($this->message2);?><br/>
<?php echo $this->escape($this->message3);?><br/>
<?php echo $this->escape($this->message4);?><br/>

</body>
</html>