すべて、MVCを使用した動的PHPウェブサイト - これはどのようなナビゲーションパターンですか?
MVCの例として私のWeb開発クラスで与えられたいくつかのサンプルコードを勉強しています(もう一度、ウェブ用です)。このコードでは、index.php
ページからさまざまなコントローラ(モデルとビューモジュールを呼び出す)にナビゲートし、index.php
に戻るシステムがあります。
私はMVCの仕組みを理解しています。
私が取り組んでいるのは、ナビゲーションメカニズムです。私はすべての作品が一緒に働く方法を理解するのが難しいです。
誰かが以下のコードを見て、これが動的なウェブサイトのナビゲーションに対処するためのよく知られた方法/パターンと一致するかどうか教えてください。 (もしかしたらフロントコントローラーかもしれない?)もしそうなら、私はもっと簡単にそれについて研究をすることができます。
多くの感謝!
JDelage
のindex.php
<?php
require_once("User.php");
session_start();
if (isset($_GET['action']))
$action= $_GET['action'];
else
$action="";
switch ($action) {
case 'login':
require_once('Login.php');
$command= new LoginControler();
break;
case 'logoff':
require_once('Logoff.php');
$command= new LogoffControler();
break;
// Several other cases
default:
require_once('Unknown.php');
$command= new UnknownControle();
}
$command->execute();
require_once('EntryMenu.php'); // Those are objects that represent both the
// menu label and the links.
$menu= array(
new EntryMenu("Login", "index.php", array("action" => "logon")),
new EntryMenu("Logoff", "index.php", array("action" => "logoff")),
new EntryMenu("Write", "index.php", array("action" => "write")),
new EntryMenu("Read", "index.php", array("action" => "read"))
);
if ($command->redirect) {
header('Location: ' . $command->redirect);
} else if ($command->page) {
include("ui/header.php");
include("ui/menu.php");
echo "<div class='content'>";
include("ui/". $command->page);
echo "</div>";
include("ui/footer.php");
}
?>
Controler.php
<?php
class Controler {
public $page= "problem.php";
function execute() {}
}
?>
LogoffControler.php
<?php
require_once('Controler.php');
class LogoffControler extends Controler {
function execute() {
$this->redirect= "index.php";
unset($_SESSION['user']);
}
}
?>
LoginControler.php
<?php
require_once('LoginModel.php'); // This manages the exchanges with the user db
require_once('Controler.php');
class ConnexionControle extends Controler {
public $page= "LoginForm.php";
function execute() {
// More code to deal with incorrectly filled login forms
$login = new LoginModel();
$login->loginUser($_POST['login'], $_POST['password']);
if ($login->userLogedIn()) {
$_SESSION['user']= $login->user;
$this->redirect= "index.php";
}
// More code to deal with invalid logins
}
}
?>
非常にクールですが、私はそれを読むことができません。 – JDelage
はい、それをすべて1KBに収める必要がありました。だから、私が言ったように、各 ";"ある行がどこで終わり、どこから始まるかを示します。代わりに、既にテスト済みで実績のある作業であるため、単純に使用することもできます。システムのコピーをダウンロードしてWebサーバーに配置して、実際の動作を確認してください。 – Xeoncross
多分私はそれをダウンロードし、行ごとに行かなければなりません。それは素晴らしい運動であるように見えます。いずれにせよ、それを書いていることをお祝いします。 – JDelage