douzhi7754 2012-06-14 11:55
浏览 27
已采纳

DRY MVC视图/控制器分离

I have a custom MVC PHP framework that has a router class, which calls a controller, which uses a model, then the controller presents the view, etc etc.

My problem is that I can't figure out technically how to allow variables to pass between the controller and the view, semantically. I could do a quick-and-dirty fix, but what I want to have is this for a controller:

class IndexController extends Controller{
    var $name = "John"; // instance variable
}

And have this for a view:

<p> <?=$name?> </p>

My question is this:

  1. How can I create a Controller->render() function, or something similar, that allows the view to access instance variables from the controller? and,
  2. How can I do this without doing klutzy things like $data['view']['name'] = "John"; or having to write ten lines of code by default for any new controller I make. I want to do this so it's as DRY as possible.

Thanks.

Edit: FabioCosta's solution

I'm not sure I understand, so far I have my base controller like this:

<?php
    class Controller{
        public function __get($key){
            if(isset($this->$$key)) return $this->$$key;
        }
    }
?>

My base view class looks like this:

<?php
    class View{
         public $controller;
         public function render(){
         $this->controller = $this;
    }
?>

And I initialize from the router like this:

<?php
    $controller = new IndexController();
    $view = new IndexView();
    $view->render();
?>

However, this doesn't work, and I know I'm doing something wrong.

  • 写回答

2条回答 默认 最新

  • dongsuikai8286 2012-06-14 12:10
    关注

    Why not pass the controller that instantiates the view and use the __get magic method?

    like so:

      public function __get($key){
    
          if(isset($this->$key)) return $this->$key;
      }
    

    Here is a working example View.php:

    class View{
       protected $_controller;
       public function __construct(Controller $controller){
            $this->_controller=$controller;
       }
       public function render(){
            echo '<h1>Hello '.$this->_controller->name.'</h1>';
       }
    }
    

    Controller.php

    class Controller{
        protected $name='fabio';
        protected $_myView;
    
        public function __get($key){
    
            if(isset($this->$key)) return $this->$key;
       }
        public function __construct(){
            $this->_myView=new View($this);
        }
    
        public function indexAction(){
            $this->_myView->render();
        }
    }
    

    And the router:

    $c=new Controller();
    $c->indexAction();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错