2017-11-15 3 views
-3

이 오류는 PHP 응용 프로그램의 MVC 모델을 작성하려고 할 때 나타납니다.MVC 모델을 빌드하는 중 오류가 발생했습니다.

Fatal error: Call to a member function render() on null in C:\xampp\htdocs\project\app\controllers\Home.php on line 13

Home.php

<?php 

class Home extends Controller{ 

    public function _construct($controller, $action) 
    { 
     parent::_construct($controller, $action); 
    } 

    public function indexAction() 
    { 
     $this->view->render('home/index') ; 
     //die('welcome home'); 
    } 
} 
?> 

View.php : 오류가 발생한 경우

<?php 

class View { 

    protected $_head,$_body,$_siteTitle=SITE_TITLE, $_outputBuffer, $_layout = DEFAULT_LAYOUT; 

    public function _construct() 
    { 
    } 

    public function render($viewName) 
    { 
     $viewAry=explode('/', $viewName); 
     $viewString=implode(DS, $viewAry); 

     if(file_exists(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php')) 
     { 
      include(ROOT . DS . 'app' . DS . 'views' . DS . $viewString . '.php'); 
      include(ROOT . DS . 'app' . DS . 'views' . DS . 'layouts' . DS . $this->_layout . '.php'); 
     } else { 
      die('The view \"'. $viewName .'\"does not exist.'); 
     } 
    } 

    public function content ($type) 
    { 
     if ($type == 'head') 
     { 
      return $this->_head; 
     } 
     elseif($type == 'body') 
     { 
      return $this->_body; 
     } 

     return false; 
    } 

    public function start ($type) 
    { 
     $this->_outputBuffer=$type; 
     ob_start(); 
    } 

    public function end() 
    { 
     if ($this->_outputBuffer=='head') 
     { 
      $this->_head= ob_get_clean(); 
     } 
     elseif($this->_outputBuffer=='body') 
     { 
      $this->_body= ob_get_clean(); 
     } 
     else 
     { 
      die('you must first run the start method.'); 
     } 
    } 

    public function siteTitle() 
    { 
     // if($this->_siteTitle =='') return SITE_TITLE; 
     return $this->_siteTitle; 
    } 

    public function setSiteTitle ($title) 
    { 
     $this->_siteTitle = $title ; 
    } 

    public function setLayout ($path) 
    { 
     $this->_layout = $path; 
    } 
} 
?> 

나도 몰라.

답변

-1

당신은 당신의 코드에 오타가 있습니다

public function _construct($controller, $action) 
{ 

parent::_construct($controller, $action); 

} 

마법의 방법 구조가 이름 앞에 두 개의 밑줄이 있어야합니다! 코드를 다음으로 변경하십시오.

public function __construct($controller, $action) 
{ 

parent::__construct($controller, $action); 

} 
관련 문제