今回は Index とは別にコントーラやアクションを定義してみる。

ログインページを作ることを目指して、名前を Login とする。

コントローラ名は LoginController となる。C:\test_apps\controllers フォルダに LoginController.php を追加し、中身は以下のようにする。
アクション名も login とした。
<?php
require_once 'Zend/Controller/Action.php';
class LoginController extends Zend_Controller_Action {
    public function loginAction() {
    }
}
次に、C:\test_apps\views\scripts の下に login フォルダを作成し、さらに作成した login フォルダ内に login.phtml を作成する。 login.phtml の中身は以下のとおり。
<html>
<head>
<title>ログイン</title>
</head>
<body>
ログイン
<p>
<form action="/zend_test/Login/login" method="post">
ユーザーID:<input type="text" name="userid"><br/>
パスワード:<input type="password"name="pwd"><br/>
<input type="submit" value="送信">
</form>
</body>
</html>

ブラウザで http://localhost/zend_test/Login/login にアクセスすると、ログインページが表示されるはずである。アクション名の login を省略するとエラーとなる。また、コントローラ名の Login まで省略すると Index ページが表示されるはずである。

次に 、送信ボタンを押すと自分自身へPOSTするようになっているので、loginAction 内にログインチェック処理を入れる。
Zend Framework のアクションメソッド内では、スーパーグローバル変数 $_POSTに、$this->getRequest()->getPost() でアクセスできる。また、入力された内容は $this->view->assign でビューに引き渡し、POST 後に入力内容を再現させることにする。ログインできた場合、単にログインできた旨のメッセージを表示させるだけとする。
<?php
require_once 'Zend/Controller/Action.php';
class LoginController extends Zend_Controller_Action {
    public function loginAction() {
        $uid = $this->getRequest()->getPost('uid');
        $pwd = $this->getRequest()->getPost('pwd');
        $this->view->assign('uid', $uid);
        $this->view->assign('pwd', $pwd);
        if ($uid == 'abc' && $pwd == '123') {
            $this->view->assign('loginMessage', 'ログインしました。');
        }
    }
}

ビューの変更は、ログインできた場合のメッセージの表示と、ユーザーID、パスワードの復元のみである。
<html>
<head>
<title>ログイン</title>
</head>
<body>
ログイン
<p>
<?php echo $this->loginMessage; ?>
<p>
<form action="/zend_test/Login/login" method="post">
ユーザーID:<input type="text" name="uid" value="<?php echo $this->uid; ?>"><br/>
パスワード:<input type="password" name="pwd" value="<?php echo $this->pwd; ?>"><br/>
<input type="submit" value="送信">
</form>
</body>
</html>

以上。