前回に引き続き、このサイトの開発を行う環境を整える手順を説明。
今回はコード周りの作りこみと、それに伴う設定の見直し。
ZendFrameworkのよくあるチュートリアルとは異なる構成で作成します。
これを書いている段階(2009/07)では、Zend_Toolsを使う方式が出始めていますが、そういうのはAkra’s DevNotes - Tutorial: Getting Started with Zend Frameworkを参考にしてください。
今回は、Moduleベースで開発・管理する方式で説明します。
というわけで、まずはフォルダを切っていきます。
まず、webapps配下に、[sixwish.jp]というフォルダを作成。
その下に[library]と[Modules]、[Vires]を作成します。さらに[library]配下にこのアプリケーションで使うクラスを配置するフォルダ[Htdocs]を作成します。
さらに[public_html]配下に[styles][js][images]を作成。
というわけで下図のような感じでフォルダ構成を作成します。
念のために、EclipseにInclude Pathを教えておきます。
「PHP Include Path」の上で右クリック、「Include Path」→「Configure Include Path...」と選びます。
すでに設定されている値をremoveして、Add Folder
下図のように[library]を二箇所選択してください。
こんな感じにしてください。
Zend_Toolsを使用すると一発でできるものをちまちまと作成します。
まず、public_htmlの配下にindex.phpを作成。
説明はコメントを参照してください。
<?php /** * The Sixwish project * * Code Contributor(s): * Fuu.Rokubou<Sixwish project> * * @copyright Copyright (C) 2009 The Sixwish project * @version $Id: section05.phtml 10 2010-01-31 16:11:31Z rokubou $ * @author $Author: rokubou $ */ /** * sixwish.jp bootstrap file * * @category sixwish * @package bootstrap * @copyright Copyright (C) 2009 The Sixwish project */ //********************************************************* // define site mode // 開発サイトと本番サイトを分けるためのフラグとして利用 $isLab = false; //********************************************************* // define path // Includeパスのための情報をここで定義 // ZEND_SERVER_SITE_PATHは、public_htmlの上位フォルダ define('ZEND_SERVER_SITE_PATH', dirname(dirname(__FILE__))); define('ZEND_SERVER_APP_ROOT_PATH', ZEND_SERVER_GUI_PATH . DIRECTORY_SEPARATOR . 'webapps'); //********************************************************* // Set include path $include_path = get_include_path(); // Windows環境下 if ( eregi( "c:", $include_path)) { define('ZEND_LIBRARY_PATH', 'C:/Zend/ZendServer/share/ZendFramework/library'); define('ZEND_APP_LIBRARY_PATH', ZEND_SERVER_SITE_PATH.DIRECTORY_SEPARATOR.'webapps'.DIRECTORY_SEPARATOR.'Htdocs'.DIRECTORY_SEPARATOR.'library'); set_include_path ('./;'.ZEND_LIBRARY_PATH.';'.ZEND_APP_LIBRARY_PATH); $isLab = true; // Coreserver環境下 } else { set_include_path ($include_path.':'.ZEND_SERVER_SITE_PATH.'/library:'.ZEND_SERVER_SITE_PATH.'/webapps/sixwish.jp/library'); } //********************************************************* // index.php request to redirect // http://domain/index.php でアクセスしてきた場合の対処 // .htaccesでやるほうが良いのかもしれない if ( !array_key_exists('REDIRECT_URL', $_SERVER) && ereg( "^/index.php", $_SERVER['REQUEST_URI'])){ header( "Location:http://".$_SERVER['SERVER_NAME'].'/'); exit(); } //********************************************************* // Setup Base URL // Zend_Controller_Front に渡す情報 $BASE_URL = substr( $_SERVER['PHP_SELF'], 0, strpos( $_SERVER['PHP_SELF'], '/index.php')); //********************************************************* // Load Zend Framework Components require_once ( 'Zend/Controller/Front.php'); require_once ( 'Zend/Controller/Action/Helper/ViewRenderer.php'); require_once ( 'Zend/Controller/Plugin/ErrorHandler.php'); require_once ( 'Zend/Layout.php'); require_once ( 'Zend/View.php'); //********************************************************* // Get front controller instance $frontController = Zend_Controller_Front::getInstance(); //********************************************************* // Load Zend Layout // Page Layoutというより、Page Templateの初期化と場所指定 // その後、コントローラに渡す処理を入れている $layoutOption = array('layout' => 'default' ,'layoutPath' => ZEND_SERVER_APP_ROOT_PATH. '/sixwish.jp/Views/layouts'); Zend_Layout::startMvc($layoutOption); //********************************************************* // Setting Action Helper ViewRenderer // Viewの補助設定 $view = new Zend_View( array('encoding' => 'UTF-8')); $view->doctype('XHTML11'); $view->headTitle('The Sixwish project'); $view->headTitle()->setSeparator(' / '); $view->isLab = $isLab; //********************************************************* // Setting Action Helper ViewRenderer // エラー画面の初期化をここで行う $errorHandler = new Zend_Controller_Plugin_ErrorHandler(); $errorHandler->setErrorHandlerModule('Error'); //********************************************************* // Set view render // ここまで設定してきたViewの情報をControllerに渡して使えるようにする $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $viewRenderer->setView($view); //********************************************************* // Setup controllers $frontController->setBaseUrl( $BASE_URL); $frontController->addModuleDirectory(ZEND_SERVER_APP_ROOT_PATH. '/sixwish.jp/Modules/'); //********************************************************* // Debug only // デバッグ時には例外が画面に表示されるようにする。それ以外のときは、ErrorHandlerに渡すことになる if ($isLab) { $frontController->throwExceptions( true); } //********************************************************* // Dispatch try { $frontController->dispatch(); } catch (Exception $e) { echo "<pre%>"; echo $e; echo "</pre>"; }
説明が手抜きですが、わからないところは調べてください。
次。これだけでは動かないので[Module]配下に、ActionとViewを作りこんでいきます。
下図のように、[Module]配下に[controllers]、[views]。
さらに、[views]配下には[scripts]、[index]と階層を作成します。
ついでに、図のようにファイルを二つ([IndexController.php]、[index.phtml])作成してください。
まず、IndexControllerの中身から
<?php /** * Sixwish.jp Contents * * Code Contributor(s): * Fuu.Rokubou<Sixwish project> * * @copyright Copyright (C) 2009 The Sixwish project * @license http://sixwish.jp/Licenses/newbsd New BSD License * @version $Id: section05.phtml 10 2010-01-31 16:11:31Z rokubou $ * @author $Author: rokubou $ */ /** * @see Zend_Controller_Action */ require_once ('Zend/Controller/Action.php'); /** * Default Index Contents action * * @category sixwish * @package Site_Sixwish_Controllers * @copyright Copyright (C) 2009 The Sixwish project * @license http://sixwish.jp/Licenses/newbsd New BSD License */ class IndexController extends Zend_Controller_Action { /** * コントローラ共通初期設定 * @return void */ public function init() { $this->view->headTitle('', 'PREPEND'); $this->view->headTitle('', 'PREPEND'); $this->view->title = ''; } /** * index Action * このサイトのTOPページのコントローラ。 * @return void */ public function indexAction() { } }
続いてViewスクリプト。
<?php /** * The Sixwish project * Sixwish main page * * @category htdocs * @package View_Script * @copyright Copyright (C) 2009 The Sixwish Project * @license http://sixwish.jp/Licenses/newbsd New BSD License * @version $Revision: 10 $ * @author $Author: rokubou $ * Contributor(s): * Fuu.Rokubou<Sixwish project> */ ?> <?php echo $this->doctype(); ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP"> <head> <?php echo $this->headTitle(); ?> </head> <body> <p>テスト</p> </body> </html>
とりあえずこれで表示はされます。
これだけだと、TOPページしか表示されない状態になるので、.htaccessを作成します。 「File」→「new」→「File」と選択、[sixwish.jp/public_html]の下に[.htaccess]というファイル名で作成します。
リファレンスガイドと同じ内容ですが、下記のようにして保存します。
# # ZendFramework rewrite engine set # RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
あとはZendFrameworkのマニュアルとかを参考に作りこんでみてください。