MVC 视图控制器


MVC view-controller

我开始学习MVC,我编写了自己的MVC模式,我只能做主控制器和主视图,但我不明白如何制作另一个控制器/动作,我想从我的主视图链接到另一个页面。所以我有下一个文件夹和下一个模拟代码:在我的索引中.php我有简单的:

<?php
ini_set('display_errors',1);
require_once 'myapp/bootstrap.php';

接下来,在我的引导程序中.php我连接我的基类视图.php、控制器.php、路由.php并运行路由函数 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
?>

在我的路由中.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>

所以,我的问题是 - 我需要在我的路由中做什么.php创建另一个具有操作的控制器,并加载另一个 veiw?以及如何在我的Main_View中编写指向另一个视图的链接?我还有一些网络表单,我需要用action="" ???写什么请帮助我,因为我无法理解自己并找到答案。

您可以在控制器中创建另一个操作,如下所示:

public function action_submit()
{
     $this->view->generate('blabla');
}

并将其链接为/main/submit或者您可以创建一个新的控制器文件并在其中放置一些操作。无论如何,看看一些框架,CodeIgniter对初学者来说是很好的,但是一旦你理解了它是如何工作的,你就不要停止它,你可以学习更复杂的框架,最终来到Symfony2/ZF2。

编辑:实际上最好先了解你的错误,它会给你更好的深入知识。关于框架 - 用 Silex 替换 CodeIgniter(是的,这是狗屎,我只记得我在第一步就用它学习了)。