0

私は目的を学習/教育するための mvc 構造を作成しています。これまでのところ、構造とコントローラーと小枝をテンプレート システムとして設定できました。

構造は次のとおりです。

  • index.php
  • コントローラー/
    • エラー.php
  • 株式会社/
    • controller_base.php
    • view_manager.php
  • ビュー/
    • 。キャッシュ/
    • エラー/
      • 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>
4

1 に答える 1

1

問題はローダーに基づいています。
小枝のドキュメントによると:

Twig_Loader_String は文字列からテンプレートをロードします。テンプレート参照はテンプレート ソース コードであるため、これはダミー ローダーです。
このローダーは、厳しい制限があるため、単体テストにのみ使用する必要があります: extends や include などのいくつかのタグは、テンプレートへの参照がテンプレートであるため、使用しても意味がありません。ソースコード自体。

そのため、渡された文字列のみを出力します。
Twig_Loader_String は、適切なローダーに置き換える必要があります。
この場合、Twig_Loader_Filesystem は完全に機能します。

$loader = new Twig_Loader_Filesystem($this->template_dir);

これで問題が解決し、MVC 構造は完全に正常に機能します。
ご覧いただきありがとうございます。

于 2013-06-21T16:21:08.940 に答える