非エンジニアのエンジニア道

非エンジニアからエンジニアに転向!その勉強の軌跡を載せていきます。

Zend Framerorkを使う その1

さて、だいぶ前にいれたままのZendについて、続きをば。

Zend FrameworkMVCについて

用語について

フロントコントローラ MVCのCに値する部分で、すべてのリクエストを最初に受けつける
アクションコントローラ リクエストに対する必要な処理を、実際に実行する
アクション リクエストに対して、アクションコントローラが行う処理のこと
ルーティング リクエストの内容を判断して、受け渡し先を決定する処理のこと
ディスパッチ アクションコントローラに、以後の処理を受け渡すこと

フォルダ基本構成

application/       アプリケーションを配置する
  ┠ models/       Modelに対応するファイルを配置する
  ┠ views/scripts/  Viewに対応するファイルを配置する
  ┗ controllers/  Controllerに対応するファイルを配置する

public/               フロントコントローラを配置する公開フォルダ
  ┠index.php
  ┗.htaccess

コントローラーについて

ためしに、indexでHello world的なものを作る。

application/controllers/IndexController.php

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

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    }

    public function helloAction()
    {
      $req = $this->getRequest();
      $this->view->assign('name', $req->getPost('name'));
    }
}

application/views/scripts/index/index.phtml

<html>
<head>
<title>Hello World!</title>
</head>
<body>
どちら様ですか?<br />

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

</body>
</html>

application/views/scripts/index/hello.phtml

<html>
<head>
<title>Hello World!</title>
</head>
<body>

こんにちは、<?php echo $this->escape($this->name);?>さん!<br />

</body>
</html>

エラー表示について

そのままエラーがどわっと出てたので、ちゃんと表示してあげる

application/controllers/ErrorController.php

<?php

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Controller/Plugin/ErrorHandler.php';

class ErrorController extends Zend_controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        switch ($errors->type) {
            case Zend_controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->assign('errorMessage', 'Controller Not Found');
                break;
            case Zend_controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->assign('errorMessage', 'Action Not Found');
                break;
            default:
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->assign('errorMessage', 'Application error');
                break;
        }

        $this->view->assign('errorDescription', $errors->exception);
        $this->view->assign('errorRequest', $errors->request->getParams());

    }
}

application/views/scripts/error/error.phtml

<html>
<head>
    <title>エラー</title>
</head>
<body>

<h1>An error occurred</h1>

<h2><?php echo $this->escape($this->errorMessage)?></h2>

<h3>stack trace:</h3>
<pre><?php echo $this->escape($this->errorDescription); ?></pre>

<h3>Request Parameters:</h3>
<pre><?php echo $this->escape(var_export($this->errorRequest, true)); ?></pre>

</body>
</html>

バーチャルホストについて

ちょっとディレクトリの位置を変更したので、手を入れる

Linux側設定

# vi /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
    DocumentRoot /var/www/html/zend/public
    ServerName http://zend.test.jp/
    <Directory "/var/www/html/zend/public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

# /etc/init.d/httpd restart

hostsファイル編集

windowsからアクセスするので、hostsファイルにも書いておく

  1. C:\Windows\System32\drivers\etc\hosts を開く
  2. 192.168.XXX.XXX test.zend.jp
  3. ブラウザでtest.zend.jpにアクセス




というわけで簡単なものを作った。
次は、zfのコマンドを使用してからやる。