开发者

Custom MVC Framework: How do I propogate data to a view?

开发者 https://www.devze.com 2023-04-13 01:39 出处:网络
I am creating a custom MVC framework. I verrrrry loosely modeled it after the codeIgniter framework, but it\'s ground-up custom for the most part.

I am creating a custom MVC framework.

I verrrrry loosely modeled it after the codeIgniter framework, but it's ground-up custom for the most part.

I'm at the point where I have URL's routing to the appropriate controller actions, but I'm stuck at the point where I generate a view that can utilize data generated by the controller.

I have views defined (static HTML with inline php ready to populate dynamic data), and I have the destructor of my base controller require()'ing the view in order to populate the browser with the view... here's the code:

public function __destruct()
{
    if ($this->bSuppressView === false)
    {
        require(APP_PATH.'views/layout/header.php');
        require(APP_PATH.'views/'.$this->sController.'/view.'.$this->sController.'.'.$this->sAction.'.php');
        require(APP_PATH.'views/layout/footer.php');
    }
}

Basically, when the controller is done executing, the teardown process of the base controller will then include the global header view, the controller's action's view, and then the global footer view, which should populate the webpage with everything for the URL that was requested...

HOWEVER, I cannot access any globally defined variables from the embedded php in the view code. In my bootstrap class, I define a bunch of local开发者_如何学Python variables such as my config variable, etc., but the view seems to consider those variables undefined. Additionally, i'm unsure how to allow the view to access data that the controller may have generated. Where do I "stick" it to make it available to the view?

Let me know if this isn't clear, and i'll update. Thanks!

UPDATE: I've discovered that while doing it this way, the "environment" of the views is within the controller object, which, as far as I can tell is a great thing! I don't have to propogate anything anywhere but in the controller, and I can use "$this->" in the views to get access to anything public or private from within the controller class!!!

That leaves the question: is this "normally" how it's done in MVC? What's the BEST way to propogate a view? I think this will suit my purposes, and I will post back if I discover a limitation to just treating the embedded view php as "within the scope of the calling controller"...


The way this is generally done, is that the view is actually an object. You pass that object you're variables, and that view object takes the template you gave it, includes it so that it's in the current scope, and grab the output into a variable using output buffering.

To give you a basic idea:

// controller object
$this->set('key','val');
$this->render('mytemplate');

// controller base class
$view = new View;
$view->setData($this->getData());

// view class
class View {
....
function render() {
    ob_start();
    include $this->resolveTemplate();
    $out = ob_get_contents();

    ob_end_clean();

    return $out;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消