初始化视图,模板和控制器以及模型是可选的

时间:2012-12-19 02:59:20

标签: php model-view-controller view controller initialization

  

当我问这个问题时,我太困了,很抱歉,无论如何要清楚地说明我准备了2个小时的问题。

我正在尝试组织我的代码并决定组织它mvc'ish(类似mvc),我不知道我是否可以遵循所有原则,但我想至少接近这一点。 / p>

我的应用程序有一个前端控制器(dunno,如果我的定义是正确的),所以我的应用程序的所有http请求将通过一个点,在我的情况下根目录中的index.php我的申请。

说过我已经设置好了,你可以想象我使用.htaccess将所有请求定向到index.php

我展开url并从中创建了一个数组,$url[]就像这样。因此,每当我像http://localhost/app/pagename这样访问我的应用时,它都会访问控制器(pagename_controller

我是这样做的:

$file = $controller_path . $page . '_controller.php';

if (file_exists($file)) {
    require $file;
    $class_name = ucfirst($page) . '_controller';
    $target = new $class_name();
}

我也把它包装在一个容器中,'装饰器模式',以备将来使用,验证可能。 像这样:

$controller = new Wrap($target);
$controller->index();

我不知道使用$controller变量名是否合适,所以请原谅我错了。

我认为我可以像这样设置我的应用程序:

  1. 用户发送请求,怎么样?通过使用应用程序意味着他/她发出一个http请求,它将加载应用程序的初始状态

    credit : phparchitect design patterns

  2. 正如您在我所需的应用程序结构图中所看到的,我只能执行将请求指向单个条目的第一部分(index.php

    现在问题是应用程序其他部分的初始化。

    截至目前,我有3个文件要设置,但我对如何设置感到困惑。

    index_controllerindex_viewTemplate

    class Index_controller {
        private $model;
        private $view;
    
        public function __construct(){
            // optional model -> $this->model = 'index' 
            $this->view = 'index'  // 
        }
    
        public function index(){
            $this->load->view($this->view)
        }
    }
    
    class Index_view {
        private $model;
        private $template;
    
        public function __construct(Model $model = null){
             $this->template = new Template('default');
        }
    
        public function view() {
             $this->template->assign('css', 'default_css'); // don't know if this is efficient
             // or $this->template->assign('header', 'default_header');
             // or $this->template->assign('sidebar', 'default_sidebar');
             // or $this->template->assign('footer', 'default_footer');
             // or any other things I want to use in the template
        }
    }
    
    class Template {
    
        public $data = array();
        private $tmpl;
    
        public function __construct($template) {
             $this->tmpl = $template . '_tmpl.php';
        }
    
        public function assign($name, $value){
            $this->data[$name] = $value;
        }
    
        // public function output
        // function that will explode the data array and render it out as a webpage
        // I'll create templates and
    }
    

    有了这个,我现在想知道如何将这些东西联系在一起。目前我有一个system文件夹,可以包含类,我为该文件夹设置了自动加载器。

    我正在考虑创建一个Controller类和View类,它充当ActionFactory和ViewFactory,如图所示,尽管我知道这些不是他们的责任。

    我在想这个:

    class Controller {
    
        protected $load;
        public function __construct() {
            $this->load = new View();
        }
    }
    
    class View {
        public function __construct() {
        // some things i don't know
        }
    
        public function view() {
        // some things i don't know
        }
    }
    

    我的设置中有什么建议和意见。我怎样才能发起三合会?

1 个答案:

答案 0 :(得分:1)

好吧,让我们不要过多地关注您的实施细节。您可以在其他时间阅读有关保护框架的信息。我们来处理你的问题......

我实际上创建了一个框架,可以按照您要实现的方式工作。我认为你缺少的是RoutingHandler类。路由是对URL的物理操作,它告诉应用程序要加载哪个Controller,以及要运行哪个Action。

在我的世界里我也有模块,所以基本的路由方案是

Module -> Controller -> Action

这三个项目以这种方式映射到我的URI方案。变量也可以像这样附加......

http://www.domain.com/module/controller/action/var1/val1/var2/val2

那么,在解析URI之后会发生什么,并将控制权传递给适当的控制器和操作?让我们编写一些代码来演示一个简单的例子......

<?php    
    class indexController extends Controller {

        protected function Initialize() {
            $this->objHomeModel = new HomeModel;

            $this->objHeader = new Header();
            $this->objFooter = new Footer();

            $this->objHeader
                ->SetPageId('home');
        }

        public function indexAction() {
            $this->objHeader->SetPageTitle('This is my page title.');
        }
    }
?>

Initialize方法中,我设置了一些控制器范围的东西,然后抓取我的模型实例以便稍后使用。真正的肉是indexAction方法。您可以在此处设置要在View中使用的内容。例如......

public function randomAction() {
    $this->_CONTROL->Append($intSomeVar, 42);
}

_CONTROL是一组值,我操纵并传递给View。 Controller类知道如何为View找到正确的模板,因为它以Action命名(并在兄弟目录中)。

Controller父类采用动作方法的名称并像这样解析...

indexAction -> index.tpl.php

你也可以在这里做一些有趣的事情,例如......

Application::SetNoRender();

...会告诉Controller不要在模板中渲染,而只是完成方法。这对于您实际上不想输出任何内容的情况非常有用。

最后,所有控制器,模型和视图都在他们自己的目录中,就像这样......

my_module
    controllers
        indexController.class.php
        someotherController.class.php
        :
        :
    models
        HomeModel.class.php
        :
        :
    templates
        index.tpl.php
        someother.tpl.php
        :
        :

我可以继续,但我是从记忆中写下来的,这里和那里都有一些皱纹,但希望这给你提供了思考的食物。

相关问题