最近我一直在学习如何在我的应用程序中实现 MVC 结构。我计划在不久的将来使用 PHP 框架,但现在它只是纯 PHP。我了解到控制器应该“瘦”而模型应该“胖”,但似乎无法理解这如何与干净的 URL 齐头并进。
在我实习的公司,我正在开发一个基于 Web 的应用程序来对数据库中的 3 个不同的表执行 CRUD 操作。每个表都有自己的特定字段,这意味着对于这些表中的每一个,都需要 4 个视图(每个 CRUD 操作 1 个)。
我想使用干净的 URL,所以现在我有 1 个(只有一个)控制器(index.php),它将 URL 分成段并分析它们,为每个段调用特定的方法和视图,或者(如果需要) ) 将用户重定向到正确的 URL。
现在控制器包含近 1000 行代码。我总是读到控制器应该“瘦”,并且看到人们在一个应用程序中谈论多个控制器。我似乎无法理解如何结合干净的 URL 来实现它......
任何帮助将非常感激。在我正在实习的公司里只有一个网页设计师,所以我不能问他任何与编程相关的问题......
下面是我的控制器的基本表示(index.php)
// Check if user is logged in
if (isset($_SESSION['username']) AND isset($_SESSION['loggedIn'])) {
// Get URL from $_SERVER
$url = $_SERVER['REQUEST_URI'];
// Split URL and assign values to $url
$url = trim($url, "/");
$url = explode('/', $url);
// Remove first url segment (index.php) for convenience
array_shift($url);
// Check if 1st URL segment (category) is set
if (isset($url[1])) {
$category = $url[1];
// Check if category is 'apples'
if ($category == 'apples') {
// Check if 2nd URL segment (action) is set
if (isset($url[2])) {
$action = $url[2];
// Check if action is 'add'
if ($action == 'add') {
// Calls to Model
// Include Presentation (form to add new record to 'apples' category)
}
// Check if action is 'view'
elseif ($action == 'view') {
// Calls to Model
// Include Presentation (list of all records in the 'apples' table)
}
// Check if action is 'edit'
elseif ($action == 'edit') {
// Check if 3d URL segment (id) is set
if (isset($url[3])) {
$id = $url[3];
// Calls to Model
// Include Presentation (form to edit record of the 'apples' category);
}
// If 3d URL segment (id) is not set then redirect
else {
header('Location: index.php/$category/view');
}
}
// Check if action is 'delete'
elseif ($action == 'delete') {
// Check if 3d URL segment (id) is set
if (isset($url[3])) {
$id = $url[3];
// Calls to Model
// Include Presentation (form to edit record of the 'apples' category);
}
// If 3d URL segment (id) is not set then redirect
else {
header('Location: index.php/$category/view');
}
}
// If 2nd URL segment (action) is invalid then assume user wants to view and redirect
else {
header("Location: index.php/$category/view");
}
}
// If 2nd URL segment (category) is not set then assume user wants to view and redirect
else {
header("Location: index.php/$category/view");
}
}
// Check if category is 'pineapples'
elseif ($category == 'pineapples') {
// same here as in 'apples' code block
}
// Check if category is 'pears'
elseif ($category == 'pears') {
// same here as in 'apples' code block
}
// If 1st URL segment (category) is invalid then redirect to index.php
else {
header('Location: index.php')
}
}
// If 1st URL segment (category) is not set then show category overview
else {
include 'presentation/category_overview.php';
}
}
// If user is not logged in then check if login form got submitted
elseif($_POST['formSubmit'] == 'submit') {
// Calls to Model (form and user credentials validation)
// Include Presentation (category overview)
}
// If user is not logged in and did not submit login form then include view (login form)
else {
include 'presentation/login.php';
}