私は目的を学習/教育するための mvc 構造を作成しています。これまでのところ、構造とコントローラーと小枝をテンプレート システムとして設定できました。
構造は次のとおりです。
- index.php
- コントローラー/
- エラー.php
- エラー.php
- 株式会社/
- controller_base.php
- view_manager.php
- controller_base.php
- ビュー/
- 。キャッシュ/
- エラー/
- view.html
- view.html
そう:
- index は twig オートローダ (および spl_register による mvc オートローダ) をインスタンス化します。
- controller_base を継承するインデックス インスタンス化エラー コントローラー。
- controller_base は view_manager を保持しています。
- error call view_manager を表示し
error/view.html
て、ブラウザに表示されるのは だけですerror/view.html
。
Apache ログにエラーはありません。( error_reporting(E_ALL)
)
Twig キャッシュ ファイルは正しく作成されましたが、内容が適切ではありません。
protected function doDisplay(array $context, array $blocks = array()) {
// line 1
echo "error/view.html";
}
誰でも理由を知っていますが、実際のビューを印刷するにはどうすればよいですか?
前もって感謝します。
コード:
index.php: オートローダーの宣言
function __autoload($class_name)
{
if(file_exists("controllers/$class_name.php")):
include strtolower("controllers/$class_name.php");
elseif(file_exists("models/$class_name.php")):
include strtolower("models/$class_name.php");
elseif(file_exists("inc/$class_name.php")):
include strtolower("inc/$class_name.php");
endif;
}
spl_autoload_register('__autoload');
require_once 'vendor/autoload.php';
Twig_Autoloader::register(); Twig のインストールは composer によって行われたため、回避されました。追加しても変化はありません。
error.php (コントローラー): 呼び出されたメソッド。
public function show($param)
{
$this->viewMng->display(get_class().$data['view'], array())
}
controller_base.php:
class base
{
protected $viewMng;
public function __construct()
{
$this->viewMng = new viewmanager();
}
}
viewmanager.php: クラス全体
class viewmanager {
private $twig;
protected $template_dir = 'views/';
protected $cache_dir = 'views/.cache';
// protected $vars = array();
public function __construct($template_dir = null) {
if ($template_dir !== null) {
// Check here whether this directory really exists
$this->template_dir = $template_dir;
}
$loader = new Twig_Loader_String($this->template_dir);
$this->twig = new Twig_Environment($loader, array(
'cache' => $this->cache_dir));
}
public function render($template_file, $data = array()) {
if (!file_exists($this->template_dir.$template_file)) {
throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir);
}
return $this->twig->render($template_file, $data);
}
public function display($template_file, $data) {
if (!file_exists($this->template_dir.$template_file)) {
throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir);
}
$tmpl = ($this->twig->loadTemplate($template_file));//print_r($tmpl);
$tmpl->display($data);
}
}
view.html:
<html><body> Hello </body></html>