上周左右我一直在阅读和学习 OOP 和 MVC,所以我想知道我是否走在正确的轨道上。
这是我目前拥有的示例:
index.php 解析从 Apache mod_rewrite (.htaccess) 传入的 URL。在这个文件中,我还首先包含了我的站点特定设置和定义的变量,然后包含了相关的控制器文件,然后是视图。
<?php
// Include the site specific settings
require 'includes/settings.php';
// Include the HTML page header
require LIBPATH . 'views/page_header.php';
//Code to parse the url passed in from mod_rewrite
require LIBPATH . 'controllers/' . $require_url . '.inc.php';
require LIBPATH . 'views/' . $require_url . '.php';
// Include the HTML page footer
require PUBLICPATH . 'includes/page_footer.php';
?>
转到控制器:在这个文件中,我确保设置了 $_POST 表单,然后调用模型(类)。
<?php
if (isset($_POST)) {
$loginUser = new User();
$loginUser->email = $_POST['email'];
$loginUser->password = $_POST['password'];
$returnArray = (json_decode($loginUser->select(), true));
$_SESSION['userID'] = $loginUser->userID;
$_SESSION['firstName'] = $loginUser->firstName;
$_SESSION['lastName'] = $loginUser->lastName;
$_SESSION['email'] = $loginUser->email;
// Redirect code to admin area of the site
}
?>
现在模型(类)代码:
<?php
// Basically I'm interacting with the database and returning the data in a JSON encoded array. This is always where I check to make sure that values are set and correct before doing the database queries.
?>
这是使用 MVC 和 PHP OOP 的正确方法吗?
谢谢您的意见。