Understanding MVC Views in PHP

后端 未结 7 778
生来不讨喜
生来不讨喜 2020-11-22 10:45

I have to seem problems grasping the concept of Views in MVC, they are, according to what I\'ve read, the layer that manages the presentation in the aplication, but many of

7条回答
  •  旧巷少年郎
    2020-11-22 11:03

    class View {

    protected $data;
    
    protected $path;
    
    protected static function getDefaultViewPath() {
        $router = App::getRouter();
    
        if(!$router){
            return false;
        }
    
        $controller_path = $router->getController();
        $method_path = ($router->getMethodPrefix() !== "" ? $router->getMethodPrefix() . '_' : '') . $router->getAction();
    
        return ROOT . "/views/" . $controller_path . "/" . $method_path . ".phtml";
    }
    
    public function __construct($data = array(), $path = null) {
    
        if(!$path){
            //default
           $path = $this->getDefaultViewPath();
        }
    
        if(!file_exists($path)){
            throw new Exception("Error view file!");
        }
    
        $this->data = $data;
        $this->path = $path;
    }
    
    
    public function render(){
        $data = $this->data;
    
        ob_start();
        include ($this->path);
        $content = ob_get_clean();
    
        return $content;
    }
    

    }

提交回复
热议问题