自定义 MVC 框架:如何将数据传播到视图?

发布于 2024-12-10 02:55:52 字数 1049 浏览 0 评论 0原文

我正在创建一个自定义 MVC 框架。

我在 codeIgniter 框架之后松散地对其进行了建模,但它的大部分内容都是从头开始定制的。

我现在已经有 URL 路由到适当的控制器操作,但我陷入了生成可以利用控制器生成的数据的视图的阶段。

我定义了视图(带有内联 php 的静态 HTML 准备填充动态数据),并且我有基本控制器的析构函数 require()'ing 视图,以便用视图填充浏览器...这是代码:

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');
    }
}

基本上,当控制器执行完成时,基本控制器的拆卸过程将包括全局标题视图、控制器操作视图,然后是全局页脚视图,该视图应使用所请求的 URL 的所有内容填充网页。 。

然而,我 无法从视图代码中嵌入的 php 访问任何全局定义的变量。在我的引导类中,我定义了一堆局部变量,例如我的配置变量等,但视图似乎认为这些变量未定义。此外,我不确定如何允许视图访问控制器可能生成的数据。我应该将其“粘贴”到哪里以使其可供视图使用?

如果不清楚,请告诉我,我会更新。 谢谢!

更新:我发现,在这样做时,视图的“环境”位于控制器对象内,据我所知,这是一件很棒的事情!除了在控制器中之外,我不需要在任何地方传播任何内容,并且我可以使用“$this->”在视图中可以从控制器类中访问任何公共或私有的东西!

这就留下了一个问题:这在 MVC 中是“正常”的实现方式吗?传播观点的最佳方式是什么?我认为这适合我的目的,如果我发现将嵌入视图 php 视为“在调用控制器的范围内”的限制,我会回复...

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 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"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2024-12-17 02:55:52

通常完成此操作的方式是视图实际上是一个对象。您传递该对象作为变量,该视图对象采用您提供的模板,将其包含在当前范围内,并使用输出缓冲将输出抓取到变量中。

给你一个基本的想法:

// 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文