2014-09-05 39 views
0

我想在整個基於Codeigniter的網站中更改輸出。CodeIgniter,發送前如何修改緩衝輸出

很簡單,我想這樣做

$output = str_replace(
    array('ā','ē','ī','ō','ū','Ā','Ē','Ī','Ō','Ū'), 
    array('a','e','i','o','u','A','E','I','O','U'), 
    $output 
) 

倘若用戶更喜歡這樣。

通過這裏讀書的問題和答案,我找到了一個鏈接,可以幫助..

https://ellislab.com/codeigniter/user-guide/general/controllers.html#output

..但它只能通過控制器的工作原理控制器,這將是內容重複。

有沒有一種方法可以製作鉤子?

在此先感謝!

+0

一切都可以歸結爲最小數量的文件。你想修改什麼?有什麼理由不能在模板控制器中處理?你應該更新你的問題與特定的情況。否則,你會被降低,這似乎已經開始 – 2014-09-05 16:33:50

+0

感謝您的信息,我只是做了 – B7th 2014-09-05 16:35:19

回答

0

在應用程序/庫文件,你可以做一個PHP文件中,你可以寫這樣的事情:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Letterconverter { 

     public function convert($value) { 

      $before= array('ā','ē','ī','ō','ū','Ā','Ē','Ī','Ō','Ū'); 
      $after =array('a','e','i','o','u','A','E','I','O','U'); 
      $value = str_replace($before, $after, $value); 
      return $value; 
     } 
    } 

然後你就可以在任何地方你想作爲一個庫調用它。

0

對,您可以在加載器類的擴展中編寫替換值,並將其保存爲application/core中的MY_Loader.php。這裏是一個例子:

<?php 

class MY_Loader extends CI_Loader 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    function view($template, $view, $vars = array(), $return = FALSE, $absolute = false) 
    { 
     /* This seems inferior, but it's the easiest solution 
     * if $view is an array then the function is being called with the default CI params, ie: 
     * $this->load->view('some_view', $data) 
     */ 
     if(is_array($view)){ 
      $vars = $view; 
      $view = $template; 
      $template = false; 
     } 

     if($template == false){ 
      parent::view($view, $vars, $return); 
     }else{ 
      if($absolute === true) 
      { 
       $template_path = 'templates/' . $template . '/' . $view . EXT; 

       if(!file_exists('application/views/'.$template_path)) 
       { 
        show_error('The requested absolute view does not exist: ' . $template_path); 
       } 

       $template = 'templates/'.$template.'/'; 
       $view_path = false; 
      } 
      else 
      { 
       $template = 'templates/'.$template.'/'; 
       $template_path = $template . 'template' . EXT; 

       if(!file_exists('application/views/'.$template_path)) 
       { 
        show_error('The requested template does not exist: ' . $template_path); 
       } 

       $view_path = $template . $view . EXT; 

       if(!file_exists('application/views/'.$view_path)) 
       { 
        show_error('The requested view does not exist: ' . $view_path); 
       } 
      } 

      $template_vars = array(); 
      $template_vars['tpl_title'] = 'Title Not Defined'; 
      $template_vars['tpl_meta'] = array(); 
      $template_vars['tpl_link'] = array(); 
      $template_vars['tpl_script'] = array(); 
      $template_vars['tpl_view'] = $view . EXT; 

      foreach($vars as $key => $val) 
      { 
       $template_vars[$key] = $val; 
      } 

      //$vars = array_merge($vars, $template_vars); 

      //include_once($template.'/template_functions' . EXT); 

      return $this->_ci_load(array('_ci_view' => $template_path, '_ci_vars' => $this->_ci_object_to_array($template_vars), '_ci_return' => $return)); 
     } 
    } 

    // -------------------------------------------------------------------- 

    /** 
    * Loader 
    * 
    * This function is used to load views and files. 
    * Variables are prefixed with _ci_ to avoid symbol collision with 
    * variables made available to view files 
    * 
    * @access private 
    * @param array 
    * @return void 
    */ 
    function _ci_load($_ci_data) 
    { 
     if(substr($_ci_data['_ci_view'], 0, 12) != './templates/'){ 
      parent::_ci_load($_ci_data); 
     }else{ 
      // Set the default data variables 
      foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) 
      { 
       $$_ci_val = (! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val]; 
      } 

      // Set the path to the requested file 
      if ($_ci_path == '') 
      { 
       $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION); 
       $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view; 
       $_ci_path = $_ci_file; 
      } 
      else 
      { 
       $_ci_x = explode('/', $_ci_path); 
       $_ci_file = end($_ci_x); 
      } 

      if (! file_exists($_ci_path)) 
      { 
       show_error('Unable to load the requested file: '.$_ci_file); 
      } 

      // This allows anything loaded using $this->load (views, files, etc.) 
      // to become accessible from within the Controller and Model functions. 
      // Only needed when running PHP 5 

      if ($this->_ci_is_instance()) 
      { 
       $_ci_CI =& get_instance(); 
       foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) 
       { 
        if (! isset($this->$_ci_key)) 
        { 
         $this->$_ci_key =& $_ci_CI->$_ci_key; 
        } 
       } 
      } 

      /* 
      * Extract and cache variables 
      * 
      * You can either set variables using the dedicated $this->load_vars() 
      * function or via the second parameter of this function. We'll merge 
      * the two types and cache them so that views that are embedded within 
      * other views can have access to these variables. 
      */ 
      if (is_array($_ci_vars)) 
      { 
       $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); 
      } 
      extract($this->_ci_cached_vars); 

      /* 
      * Buffer the output 
      * 
      * We buffer the output for two reasons: 
      * 1. Speed. You get a significant speed boost. 
      * 2. So that the final rendered template can be 
      * post-processed by the output class. Why do we 
      * need post processing? For one thing, in order to 
      * show the elapsed page load time. Unless we 
      * can intercept the content right before it's sent to 
      * the browser and then stop the timer it won't be accurate. 
      */ 
      ob_start(); 

      // If the PHP installation does not support short tags we'll 
      // do a little string replacement, changing the short tags 
      // to standard PHP echo statements. 


     $content = file_get_contents($_ci_path); 

     $content = str_replace(
      array('ā','ē','ī','ō','ū','Ā','Ē','Ī','Ō','Ū'), 
      array('a','e','i','o','u','A','E','I','O','U'), 
      $content 
     ); 

     if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) 
     { 
      echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', '.$content))); 
     } 
     else 
     { 
      echo eval('?>'.$content.'<?php'); // include() vs include_once() allows for multiple views with the same name 
     } 

      log_message('debug', 'File loaded: '.$_ci_path); 

      // Return the file data if requested 
      if ($_ci_return === TRUE) 
      {  
       $buffer = ob_get_contents(); 
       @ob_end_clean(); 
       return $buffer; 
      } 

      /* 
      * Flush the buffer... or buff the flusher? 
      * 
      * In order to permit views to be nested within 
      * other views, we need to flush the content back out whenever 
      * we are beyond the first level of output buffering so that 
      * it can be seen and included properly by the first included 
      * template and any subsequent ones. Oy! 
      * 
      */ 
      if (ob_get_level() > $this->_ci_ob_level + 1) 
      { 
       ob_end_flush(); 
      } 
      else 
      { 
       // PHP 4 requires that we use a global 
       global $OUT; 
       $OUT->append_output(ob_get_contents()); 
       @ob_end_clean(); 
      } 
     } 
    } 
} 

你可能不需要所有這些,但我只是從一個存檔的項目中複製它。注意_ci_load()函數在評估之前可以影響輸出(通常是包含,如註釋所示,但更改爲字符串)。我沒有測試過這個,所以它不打算成爲一個解決方案,因爲它是一般性的提醒,你可以在一個地方擴展核心類和修改輸出。

當然,它可能是更容易在輸出類本身做: https://ellislab.com/codeigniter/user-guide/libraries/output.html

+0

我同意,我可能會延長輸出類 – 2014-09-05 17:03:59

+0

是的。我發佈這個是因爲我在loader類中做了類似的事情,代碼很方便 – 2014-09-05 17:10:23

0

如果你的目的是要取代重音符號的話,我建議你看看WordPress的remove_accents功能here

2)看看codeigniter hooks,有display_override鉤

$hook['display_override'] = array(
    'class' => 'MyClass', 
    'function' => 'Myfunction', 
    'filename' => 'Myclass.php', 
    'filepath' => 'hooks', 
    'params' => array() 
); 

3)一旦你定義一個函數/類此掛鉤,你可以得到輸出字符串有

$this->CI =& get_instance(); 
$out = $this->CI->output->get_output(); 

和那麼你可以改變輸出,只要你喜歡...