简体   繁体   English

我如何在php中将变量传递给我的视图

[英]How can I pass variables to my views in php

I have a class which contains a method to load a view. 我有一个类,其中包含一种加载视图的方法。 This works perfect and the page is loaded correctly! 这可以完美工作,并且页面已正确加载! The problem I'm having is that I can't seem to figure out a way to pass variables to the view that my method currently tries to load. 我遇到的问题是,我似乎无法找出一种将变量传递给方法当前试图加载的视图的方法。 How can I go by passing variables to the page? 如何将变量传递给页面? Is there a way I can manipulate my current code to make this work? 有没有办法可以操纵当前代码来完成这项工作? Or am I going in the complete opposite direction? 还是我朝着完全相反的方向前进?

Controller Class: 控制器类别:

<?php
    class Controller {

        private $args = array();

        public function view($page, $args = null) {

            if ($args !== null) {
                $this->args = $args;
                extract($this->args);
            }

            $page = VIEWS . $page . '.php';
            $template = $this->getTemplate($page);

            echo $template;
        }

        private function getTemplate($file) {
            ob_start();
            include($file);
            $template = ob_get_contents();
            ob_end_clean();
            return $template;
        }
    }
    ?>

Controller: 控制器:

<?php
    class Page extends Controller {

        public function person($age) {
            $data['age'] = $age;
            $this->view('person', $data);
        }

    }
?>

View Contents: 查看内容:

</h1>My name is Dennis. My age is: <?php echo $age; ?></h1>

The end result is an undefined variable ($age). 最终结果是一个未定义的变量($ age)。

The variables which you extract in view() are not available in getTemplate() . 您在view()中提取的变量在getTemplate()中不可用。 It is a different scope. 这是一个不同的范围。 You should instead extract them in the same method that does the rendering. 您应该改用与渲染相同的方法提取它们。

Also, what you have there is not an MVC view. 另外,您所拥有的也不是MVC视图。 It's just a template . 这只是一个模板 A view in MVC is an instance which is responsible for UI logic. MVC中的视图是负责UI逻辑的实例。 It should be requesting the information, that it needs, from model layer and then, if a response is not simply a HTTP location header, creating the HTML using multiple templates. 它应该从模型层请求所需的信息,然后,如果响应不仅仅是一个HTTP位置标头,则使用多个模板创建HTML。

尝试这个:

</h1>My name is Dennis. My age is: <?php echo $data['age']; ?></h1>

The easiest way is to pass down the $args variable to getTemplate, and do the extract method in the getTemplate method 最简单的方法是将$ args变量传递给getTemplate,并在getTemplate方法中执行extract方法

private function getTemplate($file, $args = null) {
    if ($args !== null) {
        extract($args);
    }
    ob_start();
    include($file);
    $template = ob_get_contents();
    ob_end_clean();
    return $template;
}

when you include and capture the the output, that php file is executed, so before inclusion all the variables must be present 当您包含并捕获输出时,将执行该php文件,因此在包含之前必须存在所有变量

If you dont want to pass down variables you can use $this->args inside of the template 如果您不想传递变量,则可以在模板内部使用$ this-> args

You extract the variables in a different method than where you include the template. 您可以使用与模板不同的方法来提取变量。 If you want to use plain variables in your templates, make sure that they are extracted in the same scope. 如果要在模板中使用普通变量,请确保将它们提取到相同的作用域中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM