【PHP】【Zend Framework】ページ移動で Hello World

* ビューを使って Hello World の続きみたいなもの
http://blogs.yahoo.co.jp/dk521123/24253757.html

参考資料

http://codezine.jp/article/detail/1961?p=3

サンプル

フォルダ構成

「*」は、フォルダの意味で、付いてなければ、ファイルを意味する
 * アプリケーションのルートディレクトリ
 |
 +- * public(公開用ディレクトリ)
 |  +- .htaccess
 |  +- index.php
 |
 +- * application(MVCアプリケーションのフォルダ・ファイルを格納するためのディレクトリ)
      +- * controllers(コントローラ関係のファイルを格納するためのディレクトリ)
         |
         +- IndexController.php
         |
         * views (ビュー関係のファイルを格納するためのディレクトリ)
           + * scripts
              + * index
                 +- index.phtml
                 +- hello.phtml

【1】.htaccess の作成

.htaccess

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|jpeg|png|css|htm|html)$ index.php

【2】フロントコントローラの作成

index.php

<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('../application/controllers');
ini_set( 'display_errors', true);

【3】コントローラの作成

IndexController.php

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

class IndexController extends Zend_Controller_Action {

    // デフォルトのアクションメソッド
    public function indexAction()
    {
    }
    // ★ hello アクションメソッド★
    public function helloAction()
    {
      $req = $this->getRequest();
      $this->view->assign('name', $req->getPost('yourname'));
    }
}

【4】ビューの作成

* コントローラ/アクションごとに、対応するビューを「views/scripts/【コントローラ名】/【アクション名】.phtml」というファイル名で作成する

index.phtml

<html>
<body>
<form action="Index/hello" method="post">
<?php echo $this->formText('yourname','your name')?>
<?php echo $this->formSubmit('','Send')?>
</form>
</body>
</html>

ビューヘルパー・メソッド

 * formText($name, $value, $attribs):<input type="text" /> 
 * formSubmit($name, $value, $attribs):<input type="submit" />
 * formFile($name, $value, $attribs):<input type="file" />

hello.phtml

* デフォルトでは、escape() は、 PHP の htmlspecialchars() に相当する
<html>
<body>
<p>Hello ,
<?php echo $this->escape($this->name);?>
!!!</p>
</body>
</html>