我开始学习 MVC 并编写自己的 MVC 模式,我只能做主控制器和主视图,但我不明白如何制作另一个控制器/动作,我想从我的主控制器中建立一些链接-查看到另一个页面。所以我有下一个文件夹和下一个简单的代码:在我的 index.php 中,我有简单的:
<?php
ini_set('display_errors',1);
require_once 'myapp/bootstrap.php';
接下来,在我的 bootstrap.php 中,我连接我的基类 view.php、controller.php、route.php 并运行 Route 函数 run():
<?php
require_once 'base/view.php';
require_once 'base/controller.php';
require_once 'base/route.php';
include_once 'Numbers/Words.php';
Route::run(); //start routing
?>
在我的 route.php 中,我编写了这个函数 run()
<?php
class Route
{
static function run()
{
// controller and action by defalt
$controller_name = 'Main';
$action_name = 'index';
$routes = explode('/', $_SERVER['REQUEST_URI']);
// get controller name
if ( !empty($routes[1]) )
{
$controller_name = $routes[1];
}
// get action name
if ( !empty($routes[2]) )
{
$action_name = $routes[2];
}
// add prefix
$controller_name = 'Controller_'.$controller_name;
$action_name = 'action_'.$action_name;
// add file with controller class
$controller_file = strtolower($controller_name).'.php';
$controller_path = "myapp/controllers/".$controller_file;
if(file_exists($controller_path))
{
include "myapp/controllers/".$controller_file;
}
else
{
Route::ErrorPage404();
}
// create controller
$controller = new $controller_name;
$action = $action_name;
if(method_exists($controller, $action))
{
// invoke action of controller
$controller->$action();
}
else
{
Route::ErrorPage404();
}
}
function ErrorPage404()
{
$host = 'http://'.$_SERVER['HTTP_HOST'].'/';
header('HTTP/1.1 404 Not Found');
header("Status: 404 Not Found");
header('Location:'.$host.'404');
}
}
它定义了我的控制器和 acrions 路由。我也有我的 Controller_Main:
<?php
class Controller_Main extends Controller
{
function action_index()
{
$this->view->generate('main_view.php', 'template_view.php');
}
}
它加载了我的视图和模板:
<div class="title">
<h1>Paymentwall PHP Test</h1>
<h2>Number To String Convertion</h2>
</div>
<div class="convertion_form">
<form name="form" class="form" method="POST" action="main/index">
<label>Enter your Number Please:</label>
<input class="number_input" type="text" name="number_input">
<input type="submit" value="Convert">
</form>
</div>
模板:
<!DOCTYPE html>
<html>
<head>
<title>Main Page</title>
<link rel="stylesheet" href="http://localhost:81/css/style.css">
<meta charset="utf-8">
</head>
<body>
<?php include 'myapp/views/'.$content_view; ?>
</body>
</html>
所以,我的问题是 - 我需要在我的 route.php 中做什么来创建另一个具有操作的控制器,并加载另一个 veiw?以及如何将 Main_View 中的链接写入另一个视图?我也有一些网络表格,我需要写什么action=""
???请帮助我,因为我无法理解自己并找到答案。