0

我正在 PHP 中创建一个基本的 MVC 结构化 CMS,作为了解 MVC 工作原理的一种方式(因此我没有使用真正的预构建引擎)。我有一个基本版本,其结构与本教程非常相似。但是,我希望自动加载视图,绕过对模板类的需求。如果强烈建议这样做,我会坚持使用模板概念(如果有人能解释为什么它如此必要,我将不胜感激。)无论如何,下面是我的路由器类,我已经修改它以自动加载控制器中的视图文件。

    public function loader() {
        /*** check the route ***/
        $this->getPath();

        /*** if the file is not there diaf ***/
        if (is_readable($this->controller_path) == false) {
            $this->controller_path = $this->path.'/controller/error404.php';
            $this->action_path = $this->path.'/view/error404.php';
        }

        /*** include the path files ***/
        include $this->controller_path;
        include $this->action_path;

        /*** a new controller class instance ***/
        $class = $this->controller . 'Controller';
        $controller = new $class($this->registry);

        /*** check if the action is callable ***/
        if (is_callable(array($controller, $this->action)) == false) {
            $action = 'index';
        } else {
            $action = $this->action;
        }
        $controller->$action();
    }

    /**
    *
    * @get the controller
    *
    * @access private
    *
    * @return void
    *
    */
    private function getPath() {

        /*** get the route from the url ***/
        $route = (empty($_GET['rt'])) ? '' : $_GET['rt'];

        if (empty($route)) {
            $route = 'index';
        } else {
            /*** get the parts of the route ***/
            // mywebsite.com/controller/action
            // mywebsite.com/blog/hello
            $parts = explode('/', $route);
            $this->controller = $parts[0];
            if(isset( $parts[1])) {
                $this->action = $parts[1];
            }
        }
        if( ! $this->controller ) { $this->controller = 'index'; }
        if( ! $this->action ) { $this->action = 'index'; }

        /*** set the file path ***/
        $this->controller_path = $this->path .'/controller/'. $this->controller . '.php';
        $this->action_path = $this->path .'/view/'. $this->controller . '/'. $this->action . '.php';
    }

这会阻止我的视图文件加载控制器给出的变量(教程网站对此有更好的演示)但是在设置$this->registry->template->blog_heading = 'This is the blog Index';视图时不会加载它,因为 template.class 被绕过了。基本上我要问的是如何将 template.class 转移到加载函数中?

4

3 回答 3

3

“视图只是一个愚蠢的模板”这一相当普遍的误解主要是由 Ruby-on-Rails 和遵循其损坏的 ORM-Template-Adapter 的那些长期存在的。我不能直截了当地将它们实现为模型-视图-控制器...

视图应该处理 MVC 和受 MVC 启发的设计模式模式中的表示逻辑。这使它们成为对象,具有处理多个模板的能力。

根据您在 Web 应用程序中使用的受 MVC 启发的模式(使用 PHP 实现经典 MVC 是不可能的),您的视图要么从类似控制器的结构(在 MVP 和 MVVM 模式中)接收数据,要么能够直接请求信息来自模型层(Model2 MVC 和 HMVC 模式)。我个人更喜欢从模型层获取数据的活动视图。

像这样的 PS 代码$this->registry->template->blog_headingDemeter流血了。

PPS 关于如何实现纯 php 模板,请阅读这篇文章

于 2012-07-24T10:56:52.957 回答
1

在我自己开发的 MVC 中,加载视图的工作方式与您所拥有的类似,但比您链接到的示例提供的方式要简单得多。(当我第一次决定尝试学习 MVC 时,我看了那个例子,我记得它把我弄糊涂了 x;。

本质上,在您确定该文件存在之后,您只需要求(是的要求,我觉得找不到文件是在这种情况下停止执行脚本的一个很好的理由)该文件。

所以..而不是整个模板类的东西(我希望我不会回避你的问题并且离基础太远,这是一个让控制器打开视图文件的简单示例。

<?php
class Pizza_Shop_Controller extends Base_Controller
{
    public function index()
    {
        $data['users'] = array('bob','lisa','bertha');
        $data['some_string'] = "Whoa I'm a string!";

        $this->render_view('index',$data);
    }

    public function contact()
    {
        if($_POST)
        {
            Contact::process($_POST);
            return $this->render_view('contact_success');
        }
        else
        {
            return $this->render_view('contact_form');
        }
    }


}

class Base_Controller
{

    protected function render_view($view_name,$data = array())
    {
        /*
         * I also think render_view should take care of loading the layout, and then inject the content into the middle of the layout file,
         * so that you aren't trapping yourself to a specific layout, and repeating the header and footer inside of every view file
        */

        extract($data); //places all $data variables into the local scope.. very clean and ezy ;].
        require($this->root_directory.DS."$view_name.php");
    }

    /**********************************/
    public function _no_action($view_name) //Called if there is no corresponding action
    {
        /* You can use method_exists to test if a method exists within the controller, if it does not exist,
         * you can then call this function, and pass it the name of the view that is attempting to be opened
         */
         if($this->view_exists($view_name))
         {
            $this->render_view($view_name,$data);
         }
         else
         {
            $this->render404();
         }
    }

}
于 2012-07-24T08:40:58.627 回答
1

我知道这对你现在没有多大帮助,但几个月前我遇到了同样的问题。这是基于我构建的框架:

https://github.com/andyhmltn/Cherry-Framework-Blog-Example/

我不确定它在哪里或如何做,因为我已经有一段时间没有真正看过它了,但是四处看看,加载控制器、设置变量然后加载视图的代码可能在库文件夹。它允许您这样做:

/** Controller **/
class ExampleController extends Controller {
    public function index() {
        $helloworld = 'Hello world';
        $this->set('hello_world', $helloworld);
        #Renders view automatically
    }
}

/** View **/
echo $hello_world;
于 2012-07-24T08:54:05.023 回答