Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php RenderToHtml视图文件并返回字符串?_Php_Model View Controller - Fatal编程技术网

Php RenderToHtml视图文件并返回字符串?

Php RenderToHtml视图文件并返回字符串?,php,model-view-controller,Php,Model View Controller,我制作了一个小的MVC框架,但是我需要我的视图类能够以字符串的形式返回页面,但在此之前应用所有变量。如何做到这一点 class View{ private $registry; private $vars = array(); private $file; public function __set($index, $value){ $this->vars[$index] = $value; } public functio

我制作了一个小的MVC框架,但是我需要我的视图类能够以字符串的形式返回页面,但在此之前应用所有变量。如何做到这一点

class View{
    private $registry;
    private $vars = array();
    private $file;
    public function __set($index, $value){
        $this->vars[$index] = $value;
    }

    public function  __construct($controller, $action){
        $this->file = "Views/".$controller."/".$action.".php";
        $this->registry = new Registry();
    }

  public function renderToHtml(){
        try{
            if(file_exists($this->file)){

                foreach ($this->vars as $key => $value){
                   $$key = $value;
                }

                /** include $this->file; **/ 
//apply vars on file 
                return $html;
            }else{
                throw new Exception("No such View file");
            }
        }catch (Exception $e) {
            echo '<h1>Caught exception: ',  $e->getMessage(), "</h1>";
            exit;
        }
    }

因此,htmlview将是常规indexView中的一部分,看起来您需要获取文件内容并替换其中的所有变量。

使用

例如:

ob_start();
/* your error catch */
$render_html = ob_get_flush();
return $render_html;

是的,这将起作用,但include$this->file仍将在父视图中包含视图文件,因此它将以参数形式显示为2xPass,指向“$html->renderToHtml($ob=true);”因此,您可以决定何时执行ob_start()?
ob_start();
/* your error catch */
$render_html = ob_get_flush();
return $render_html;