2011-06-25 6 views
1

私はこの質問をどのようにするのかよく分かりません。基本的には、私のビューオブジェクトをSmartyオブジェクトから拡張したシングルトンにしようとしています。私はその後、ビューオブジェクトから自分のコントローラオブジェクトまで拡張することができるようにしたいと考えています。PHP Smartyシングルトンクラスを拡張する

Viewオブジェクトは、私のすべてのコントローラで使用できるテンプレート変数を割り当てます。

私は今何が問題になっているのか知っていますが、誰かが正しい方向に向けることができれば、それはすばらしいでしょう。私はこれを私ができる最高の言葉にしようとしました。

<?php 

defined('SITE_ROOT') ? null : define('SITE_ROOT', $_SERVER['DOCUMENT_ROOT'].'/mvcsandbox'); 

require_once('Smarty/Smarty.class.php'); 

class View extends Smarty { 

    public static $instance=NULL; 

    public static function getInstance(){ 

     if (self::$instance === null){ 
      self::$instance = new self(); 
     } 
     return self::$instance; 
    } 

    public function __construct() { 

     $this->template_dir = SITE_ROOT.'/Library/tmp/'; 
     $this->compile_dir = SITE_ROOT.'/Library/tmp/'; 
     $this->config_dir = SITE_ROOT.'/Library/tmp/'; 
     $this->cache_dir = SITE_ROOT.'/Library/tmp/'; 

     $this->assign("var1", "This is from the view class"); 

    } 

    public static function output() { 

     self::display('test.tpl'); 

    } 

} 

class Controller1 extends View { 

    public function init() { 
     $this->assign("var2", "This is from the Controller1 class"); 
    } 

} 

class Controller1_index extends Controller1 { 

    public function init() { 
     $this->assign("var3", "This is from the Controller1_index class"); 
    } 

} 

//$view = View::getInstance(); 
$controller1 = Controller1::getInstance(); 
$controller1_index = Controller1_index::getInstance(); 

$controller1->init(); 
//$controller1_index->init(); 

$controller1->output(); 

?>

+1

私はあなたが "シングルトン"を意味すると思います:D類推は印象的ですが。 – Halcyon

+1

Smartyを落として[PHPのネイティブ機能](http://codeangel.org/articles/simple-php-template-engine)を使用しようと正直にお勧めしますが、ええ、 "Simpleton" .html)をテンプレート化します。 –

答えて

5

あなたのコントローラがIEWを拡張するはずの...それは、ビューにデータを提供する必要があります。おそらくあなたは別のもののために複数のコントローラを持っていてもいなくても、いつでもロジックを他のコントローラから別のコントローラにreqouteする必要があるかもしれません。その代わりに、シングルトンであるフロントコントローラーを使用し、実際にモジュール固有のコントローラロジックを実際に処理するアクションコントローラーを使用します。

class Controller { 
    // put any shared logic between all controllers here 

} 

class FrontController extends Controller { 

    protected $request; // a model representing and http request 
    protected $response; // a model representing a http response 
    protected $view; // the view instance 
    protected static $instance; // the FrontController instance 

    protected function __construct(); 

    public static function getInstance(); 

    public function getRequest(); 

    public function getResponse(); 

    public function getView(); 

    public function execute($args); 


} 

class View { 
    protecect $engine; 

    public function __Construct($options) 
    { 
    // $options could contain overrides for smarty config 
    $options = array_merge(array(
     'template_dir' = SITE_ROOT.'/Library/tmp/', 
     'compile_dir' = SITE_ROOT.'/Library/tmp/', 
     'config_dir' = SITE_ROOT.'/Library/tmp/', 
     'cache_dir' = SITE_ROOT.'/Library/tmp/', 
    ), $options); 

    $this->engine = new Smarty(); 

    foreach($options as $name => $value){ 
     $this->engine->$name = $value; 
    } 
    } 

    public function getEngine(){ 
     return $this->engine; 
    } 

    // proxy method calls and accessors not directly defined on 
    // the View to the Smarty instance 

    public function __get($key) 
    { 
    return $this->engine->$key; 
    } 

    public function __set($key, $value){ 
    $this->engine->$key = $value; 
    return $this; 
    } 

    public function __call($method, $args){ 
    if(is_callable(array($this->engine, $method)){ 
     return call_user_func_array(array($this->engine, $method)); 
    } 
    } 

    public function render(){ 
    // render the entire smarty instance 
    } 

} 

class ActionController extends Controller 
{ 
    protected $view; 

    public function init(); 

    public function setRequest(); 

    public function getRequest(); 

    public function getResponse(); 

    public function execute($request, $response); 

    public function getView(); 

    public function __get($key){ 
    return $this->view->$key; 
    } 

    public function __set($key, $value){ 
    $this->view->$key = $value; 
    } 
} 

だからあなたのindex.phpは、それがexecuteにオプションを経由して注入する必要がどんなリクエストのライフサイクルのためのオプションFrontController::getInstance()->execute();注入を呼び出します。コンストラクターは、ビュー、要求、および応答インスタンスを事前に構成します。 Executeはロードして実行するActionControllerを決定します。そのアクションコントローラをロードしてビューを設定し、そのアクションコントローラ内のどのアクションを実行して実行する必要があるかを判断します。

あなたのアクションコントローラーがあなたのフロントコントローラーを実行した後にView::render()を呼び出すと、完全なhtmlストリングがブラウザに出力され、レスポンスオブジェクトのコンテンツとして設定されます。ヘッダーやクッキーのようなもの、そうでないものフロントcontollerは、レスポンスオブジェクトの上にsendResponseのようなものを呼び出し、ヘッダーとコンテンツをブラウザに送ります。

+0

ありがとう!私は少しそれを変更しました。私のオリジナルアイデアとあなたが私に見せたものとの間のマージをより多くしました。 –