Hello World
zfコマンドでプロジェクトの雛形を作成する
C:\Tools\Works\php>zf create project sample01
Creating project at C:/Tools/Works/php/sample01
Note: This command created a web project, for more information setting up your VHOST, please see docs/README
C:\Tools\Works\php>
ディレクトリ一覧確認
sample01
+.zfproject.xml
+application
+Bootstrap.php
+configs
+configs\application.ini
+controllers
+ErrorController.php
+IndexController.php
+models
+views
+helpers
+scripts
+error
+error\error.phtml
+index
+index\index.phtml
+docs
+README.txt
+library
+public
+.htaccess
+index.php
+tests
+application
+controllers
+IndexControllerTest.php
+tests
+bootstrap.php
+library
+phpunit.xml
フロントコントローラー
<?php
// Define path to application directory
// Define application environment
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?
getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
realpath(APPLICATION_PATH
. '/../library'), )));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
.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]
アクションコントローラー
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
}
public function helloAction()
{
// リクエストパラメータを取得
$request = $this->getRequest();
$this->view->assign('name', $request->getPost('name'));
}
}
ErrorController.php
<?php
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
$errors = $this->_getParam('error_handler');
if (!$errors || !$errors instanceof ArrayObject) {
$this->view->message = 'You have reached the error page';
return;
}
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$priority = Zend_Log::NOTICE;
$this->view->message = 'Page not found';
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$priority = Zend_Log::CRIT;
$this->view->message = 'Application error';
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->log($this->view->message, $priority, $errors->exception); $log->log('Request Parameters', $priority, $errors->request->getParams()); }
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog()
{
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
}
モデル
ビュー
index.phtml
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <form action="/index/hello" method="post"> 名前を入力してください
<?php echo $this->formText("name", "", array('maxlength' => "10", 'size' => "20")); ?>
<?php echo $this->formSubmit("", "Submit"); ?>
hello.phtml
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <?php echo $this->name; ?>
</h1>さん
</h1> <form action="/index/index" method="post"> <?php echo $this->formSubmit("", "戻る"); ?>
動作確認
ドキュメントディレクトリをapacheに設定
<VirtualHost *:80>
DocumentRoot "C:/Tools/Works/php/sample01/public"
ServerName .local
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development
<Directory "C:/Tools/Works/php/sample01/public">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
確認
初期画面
入力して、Submit
別画面に表示
「戻る」ボタンで、戻っていることを確認
サンプルファイル
最終更新:2012年09月17日 17:52