2015-03-31 59 views
1

我使用Kohana View_Core衍生作品來實現我的觀點。我想在關機時顯示緩衝視圖或自定義視圖,例如Kohana - 在關機時顯示緩衝視圖或自定義視圖

  1. 如果有運行時間=>顯示錯誤頁面
  2. 如果沒有任何錯誤執行期間某些錯誤/異常=>將顯示緩衝輸出

View

class View 
{ 

private $file = ''; 

private $data = array(); 

private static $global_data = array(); 

public static $view = ''; 

private function __construct($file = FALSE, array $data = array()) 
{ 
    if ($file !== FALSE) 
    { 
     $this->set_filename($file); 
    } 

    if (!empty($data)) 
    { 
     $this->data = $data + $this->data; 
    } 
} 

/** 
* Creates new view object and returns it. 
* 
* @param string filename 
* @param array variables 
* @return object View 
*/ 
public static function factory($file = FALSE, array $data = array()) 
{ 
    return new View($file, $data); 
} 

/** 
* Captures the output that is generated when a view is included. 
* The view data will be extracted to make local variables. This method 
* is static to prevent object scope resolution. 
* 
* @param string filename 
* @param array variables 
* @return string 
*/ 
public static function capture($view_filename, array $view_data) 
{ 
    extract($view_data, EXTR_SKIP); 

    ob_start(); 

    try 
    { 
     require $view_filename; 
    } 
    catch (Exception $error) 
    { 
     $ob_handlers = ob_list_handlers(); 

     if (!empty($ob_handlers)) 
     { 
      ob_end_clean(); 
     } 

     throw $error; 
    } 

    return ob_get_clean(); 
} 

/** 
* Load view file 
* 
* @param string filename 
* @return boolean 
* @return object View 
*/ 
public function set_filename($file) 
{ 
    if (strpos($file, APP_DIR) === FALSE) 
    { 
     $extension = strrpos($file, '.') === FALSE ? '.php' : ''; 

     $path = APP_DIR.DIR_SEP.'system'.DIR_SEP.'views'.DIR_SEP.$file.$extension; 
    } 
    else 
    { 
     $path = $file; 
    } 

    if (!file_exists($path)) 
    { 
     Error::throw_throwable('Unable to find file '.$path); 
    } 

    $this->file = $path; 

    return $this; 
} 

/** 
* Sets a global variable, similar to the set() method. 
* 
* @param string variable name 
* @param mixed variable value 
* @return object View 
*/ 
public static function set_global($key, $value = FALSE) 
{ 
    self::$global_data[$key] = $value; 
} 

/** 
* Assigns a variable by name. 
* 
* @param string variable name or an array of variables 
* @param mixed variable value 
* @return object View 
*/ 
public function set($key, $value = FALSE) 
{ 
    if (is_array($key)) 
    { 
     foreach ($key as $name => $value) 
     { 
      $this->data[$name] = $value; 
     } 
    } 
    else 
    { 
     $this->data[$key] = $value; 
    } 

    return $this; 
} 

/** 
* Renders the view object to a string. 
* 
* @throws exception 
* @param string filename 
* @return string 
*/ 
public function render($file = FALSE) 
{ 
    if ($file !== FALSE) 
    { 
     $this->set_filename($file); 
    } 

    if (empty($this->file)) 
    { 
     Error::throw_throwable('Unable to find file '.$this->file); 
    } 

    $data = array_merge(View::$global_data, $this->data); 

    return View::capture($this->file, $data); 
} 

public function __toString() 
{ 
    try 
    { 
     $result = $this->render(); 

     return $result; 
    } 
    catch (Exception $error) 
    { 
     Error::throw_throwable($error); 
    } 
} 

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

public function __get($key) 
{ 
    return isset($this->data[$key]) ? $this->data[$key] : FALSE; 
} 

} 

用法

$content = View::factory('main/Main') 
      ->set('order', !empty($_SESSION['order']) ? $_SESSION['order'] : FALSE) 
      ->set('order_success', $order_success) 
      ->set('items', $items) 
      ->set('weights', $weights) 
      ->set('ammounts', $ammounts) 
      ->set('prices', $prices) 
      ->set('total_price', $total_price) 
      ->set('validator', FALSE) 
      ; 

$template = $this->get_template(); 

echo $template->set('content', $content); 

以前的代碼行是在模板視圖中回顯一個內容視圖。這也應該是我的關機處理程序在必要時回顯的結果。有沒有一個很好/簡單的方法來做到這一點?

回答

1

聽起來像你想要的是當出現錯誤時顯示自定義錯誤頁面。我通常在這裏做的是擴展Kohana_Kohana_Exception類(如Kohana_Exception)並覆蓋public static function handler方法。

這可以讓你放入一些代碼來檢查錯誤是什麼(HTTP_404_Exception或其他),環境是什麼(dev/production),並執行你想要的任何行爲。

在我的情況下,這樣的事情:

// If not production and not an HTTP exception 
if (! ($e instanceof HTTP_Exception) && Kohana::$environment !== Kohana::PRODUCTION) 
{ 
    // Use built in error handler to show stace trace for developers 
    Kohana_Kohana_Exception::handler($e); 
    // ... exit(1); 
} 

// Otherwise 
$injected_routes = array(Route::get('error')); 
echo Request::factory('error-uri', NULL, FALSE, $injected_routes) 
       ->headers(Request::$initial->headers()) 
       ->execute() 
       ->send_headers(TRUE) 
       ->body(); 

在這裏,你需要一個名爲匹配error-uri,去到另一個錯誤控制器/動作/視圖,你可以用它來使您的自定義錯誤頁error另一條路線。


對於這個工作,你需要有通過傳遞'errors' => TRUEKohana::init呼叫bootstrap.php啓用的錯誤。

+0

「View_Class」背後的概念似乎是完美的 - 我把它完好無損。但是,我爲此編寫了一個包裝器 - 另一個靜態的'View_Class'方法,該方法檢查錯誤數組是否爲空。但是,如果您將Kohana框架作爲一個整體來使用,這也值得一看。 – sitilge 2015-04-07 09:48:35

+0

使用Kohana進行檢查。像魅力一樣工作,會嘗試重現/重寫我自己的課程。 :) – sitilge 2015-04-07 13:07:02