本文介绍了扩展PHP Smarty Singleton类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定如何提出这个问题。基本上我试图让我的视图对象从Smarty对象扩展单例。然后我想要能够扩展视图对象到我的控制器对象。



View对象将分配我想要的所有控制器的模板变量。 / p>

我知道我现在有什么问题,但如果有人可以指出我的方向,那将是真棒。

 <?php 

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

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

类View扩展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');

}

}

类Controller1 extends View {

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

}

类Controller1_index extends Controller1 {

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

}

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

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

$ controller1-> output();

?>

解决方案

你的控制器不应该扩展iew ..它应该提供数据给视图。你也许可能有多个控制器的不同的东西,在任何时候你可能需要将逻辑到另一个这样的控制器的任何一个控制器,使基本控制器是一个好主意。

  class Controller {
//在所有控制器之间放置任何共享逻辑

}

类FrontController extends Controller {

protected $ request; //一个模型表示和http请求
protected $ response; //表示http响应的模型
protected $ view; //视图实例
protected static $ instance; // FrontController实例

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可以包含smarty配置的覆盖
$ 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;
}

//代理方法调用和访问器没有直接定义在
//查看Smarty实例

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实例
}

}

类ActionController扩展Controller
{
protected $ view;

public函数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;
}
}

将调用 FrontController :: getInstance() - > execute(); 为请求生命周期注入任何选项,通过执行。构造函数将预配置视图,请求和响应实例。 Execute将确定要加载和运行的ActionController。它将加载该动作控制器并设置视图,然后确定该动作控制器中需要运行哪个动作并执行它。



动作控制器执行后你的前台控制器会调用 View :: render(),它将返回完整的html字符串输出到浏览器,并将其设置为响应对象上的内容,以及使用方法对该响应对象设置的东西,如头和cookie和什么不。然后,前轮廓仪将在响应对象上调用 sendResponse ,然后将头部和内容发送到浏览器。


I'm not really sure how to ask this question. Basically I am trying to make my view object a singleton extended from the Smarty object. I then want to be able to extend off of the view object to my controller objects.

The View object will assign my template variables I want available to all my controllers.

I know what I have now has problems, but if someone could point me in the right direction, that would be awesome. I tried to word this the best I could.

<?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();

?>

解决方案

You controller shouldnt extend the iew.. it should provide data to the view. You also probably wan tto have multiple controllers for different things and at anytime you may need to reqoute the logic to any one of these controllers from another so making the basic controller a singleton isnt a good idea. Instead use a front controller that is a singleton and then action controllers that actually handle the module specific contoller logic.

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;
   }
}

So your index.php would call FrontController::getInstance()->execute(); injecting whatever options for the request life cycle it needs to inject via the options to execute. The constructor would pre configure a view, request, and response instance. Execute will determine the ActionController to load and run. It will load that action controller and set the view, and then determine which action within that action controller needs to be run and execute it.

Your after the action controller is executes your front controller would call View::render() which would return the complete html string to output to the browser and set it as the content on the response object as well as using method on that response object to set things like headers and cookies and what not. The front contoller would then call something like sendResponse on the response object which would then send the headers and the content to the browser.

这篇关于扩展PHP Smarty Singleton类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:51