PHP 制作 vol.2

【授業内容】
modelとviewをつくってみる
(構造化Step1.2は先日の授業参照)
☆構造化Step3:Modelのファイルの書き方。

【概念】[なんで関数をexecuteに統一する必要があるの?]
[画像挿入予定地]


☆構造化Step4:Modelの中でviewを呼ぶ

【概念】[Modelのファイルがviewを引っ張ってくる]
[画像挿入予定地]


☆※エラーメッセージを出すために…

【概念】[Modelで処理された内容をviewで表示させたい!]
[画像挿入予定地]



【実行ファイル】

 ○actions/login.php

<?php
class Login
{
    //エラー表示のためにメンバ変数用意
    public $err_data = array();

    //login画面のロジックスタート
    public function execute() {
        session_start();
        //DBと接続
        $link = mysql_connect('localhost','root');

        //初期値を設定
        define('ERR_MSG_LOGIN','ログインIDまたはパスワードが間違っています。');
        define('ERR_MSG_NO_LOGIN_ID','ログインIDが未入力です');
        define('ERR_MSG_NO_LOGIN_PASS','パスワードが未入力です');
        define('ERR_MSG_LINK','接続に失敗しました');
        define('ERR_MSG_SELECTED','データベース切り替えに失敗したか、データベースが存在しません。');
        define('ERR_MSG_SQL','SQL文でエラーが発生してます。');
        $this->err_data = array(
                          'is_error' => false,
                          'err_msgs' => array()
                          );



        //関数が書かれているファイルを開く。
        require_once(WEB_ROOT_DIR.'/lib/library.php');

        //ログインの入力値が正しいか否かを判定する。

        //ログインボタンが押されたら
        if (isset($_POST["submit"])) {

            $this->err_data = library_set_valid_result($this->err_data);
            if ($this->err_data['is_error']) {

            } else {

                if (library_is_connect($link)) {
                    $this->err_data = library_check_login($link,$this->err_data);
                    if ($this->err_data['is_error'] !== true) {
                        // ログインがOKな場合はページ遷移する。
                        $_SESSION["login"] = $_POST["login"];
                        header("Location:/index.php?module=account&action=account_list");
                        exit();
                    } else {

                    }

                } else {

                }
                mysql_close($link);
            }
        }
    }

    //HTMLファイルの呼び出し。
    public function render($path) {
        require_once($path);
    }

}
?>


 ○view/login.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ログイン</title>
<link href="../css/backend.css" rel="stylesheet" type="text/css" />
<link href="../css/literal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="layout">
    <div id="header">
        <div id="headerbody">
            <div class="logo">
                <h1>Stepsシステム<br />管理者画面</h1>
            </div>
        </div>
    </div>

    <div id="contents">
        <div id="contentsbody">
            <div id="main">
                <div id="mainbody">
                    <h2>ログイン</h2>
                    <p><span class="red weight-bold">[!]ご注意ください</span>
                       <br>・必ず利用者自身のアカウントで作業をして下さい。
                       <br>・PCから離れる場合はブラウザを終了するか、PCをロックして下さい。
                    </p><br>
                    <div class="main_area">
                    <!-- ログインフォーム開始 -->
                        <form  action="" method="POST">
                                 <?php if ($this->err_data['is_error'] === true): ?>
                                <?php foreach ($this->err_data['err_msgs'] as $err_msg): ?>
                                <p class="error">
    <?php  echo $err_msg; ?>
                                </p>
                                <?php endforeach; ?>
                            <?php endif; ?>
                            <table border="0" cellspacing="1" cellpadding="0" summary="フォーム" class="tlayout">
                                <tr>
                                    <th class="content">ログインID</th>
                                    <td class="content"><input name="login" type="text"/></td>
                                </tr>
                                <tr>
                                    <th class="content">パスワード</th>
                                    <td class="content"><input maxlength="12" name="password" type="password" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2" class="center"><input name="submit" value="ログイン" type="submit" /></td>
                                </tr>
                            </table>
                        </form>
                    <!-- ログインフォーム終了 -->
                    </div>
                    <div class="border"><p><span class="orange weight-bold">[※]ご利用に際して</span><br>
   ・Stepsサイトで使用される全てのデータは目的の用途以外で複製・保存、閲覧することは堅く禁止されています。
   <br>・Stepsは、個人情報保護法で定められる個人情報取扱事業者です。
         Stepsバックエンドサイト利用者は、個人情報の不正な利用や漏洩を防ぎ、個人情報データの正確さを保つ義務があります。<br>
<span class="weight-bold">お問い合わせ先</span>
<br>会社名: 株式会社Steps  住所:東京都渋谷区 渋谷TKビル7F
<br>この件に対する問い合わせ窓口(担当者): ●●
<br>電話: 03-xxxx-xxxx FAX: 03-xxxx-xxxx 電子メール: hoge@dino.co.jp</p>
                    </div>
                </div>
            </div>
            <div class="clear">
                <p></p>
            </div>
        </div>
    </div>
    <div id="footer">
        <div id="footerbody">
            <p>&nbsp;</p>
        </div>
    </div>
    <div id="copyright">
        <p><a href="http://www.dino.co.jp" target="_blank">copyright 2009 (C) All rights reserved,Steps Inc.</a> </p>
    </div>
</div>

</body>
</html>


 ○controller.php

<?php
class Controller
{
    //メンバ変数
    public $module;
    public $action;

    //1.index.phpから飛んできたmodule.actionをセットする
    public function __construct() {
        $this->module = $_GET["module"];
        $this->action = $_GET["action"];
    }

    //2.呼び出されてるファイルを探索して、呼び出す
    public function dispatch() {
        //指定されたmodule,actionを元にファイルが存在するかチェック
        //※出来るだけエラーチェックを行い、エラーがあったときの処理を考える、
        $act_file_name = sprintf(WEB_ROOT_DIR.'/modules/%s/actions/%s.php'
                                        ,$this->module,$this->action);
        $view_file_name = sprintf(WEB_ROOT_DIR.'/modules/%s/view/%s.php'
                                        ,$this->module,$this->action);

        if (file_exists($act_file_name) !== true) {
            $this->module = 'error';
            $this->action = 'not_found_action';
        } else {

            //指定されたPHPファイルをrequire_once
            require_once($act_file_name);
            
            //指定されたPHPファイルのインスタンスを作成する。
            $action = new $this->action();
            //指定されたPHPファイル内の関数を呼ぶ。
            $action->execute();
            $action->render($view_file_name);
        }
    }
}

?>