一步一步重寫 CodeIgniter 框架 (6) —— 實現在控制器Controller中加載View


1. 控制器將模型類獲得的數據,傳遞給視圖進行顯示,所以視圖必須負責接收數據,另外重要的一點是當模型和視圖分開后,多個模型的數據可以傳遞給一個視圖進行展示,也可以說一個模型的數據在多個不同的視圖中進行展示。所以CodeIgniter 框架視圖的接口有兩個重要參數,

public function view($view, $vars = array(), $return = FALSE)

$view 即使加載哪一個視圖,$vars 即是傳入的數據, $return 即表示是直接輸出還是返回(返回可以用於調試輸出)

2. 為了達到很好的講述效果,我們直接參看 CodeIgniter類中的 代碼

function view($view, $vars = array(), $return = FALSE) {
  return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_objects_to_array($vars), '_ci_return' => $return));
}

它用到兩個輔助函數,先看簡單的

/**
* Object to Array
*
* Takes an object as input and converts the class variables to array key/vals
*
* @param    object
* @return    array
*/
protected function _ci_object_to_array($object)
{
  return (is_object($object)) ? get_object_vars($object) : $object;
}

如果 $object 是對象的話,則通過 get_object_vars 函數返回關聯數組, 這個可以作為平時的小積累。

再看 _ci_load 函數

public function _ci_load($_ci_data) {


        // 通過 foreach 循環建立四個局部變量,且根據傳入的數組進行賦值(如果沒有,則為FALSE)
        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];
        }

        $file_exists = FALSE;

        // 設置路徑, 單純加載視圖的時候 ,_ci_path 為空,會直接執行下面的 else 語句
        if ($_ci_path != '') {
            $_ci_x = explode('/', $_ci_path);
            $_ci_file = end($_ci_x);
        } else {

            // 判斷 擴展名,如果沒有則加上.php 后綴
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
            $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;

            // 搜索存放 view 文件的路徑
            foreach ($this->_ci_view_paths as $view_file => $cascade) {
                if (file_exists($view_file.$_ci_file)) {
                    $_ci_path = $view_file.$_ci_file;
                    $file_exists = TRUE;
                    break;
                }

                if ( ! $cascade) {
                    break;
                }
            }
        }

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

        include($_ci_path);
    }

這里我們針對最簡單的加載 view 的需求,抽取了完成基本 view 的代碼,從以上代碼可以看到,加載 view 其實很簡單,include 即可。

include 之前只是簡單對傳入的 視圖名作擴展名處理,以達到加載默認 .php 后綴的視圖時不需要包含.php ,而像 $this->load->view('test_view');

3. 我們將使用 CodeIgniter 中視圖的例子

在views 下面新建一個文件
test_view.php

<html>
<head>
<title>My First View</title>
</head>
<body>
 <h1>Welcome, we finally met by MVC, my name is Zhangzhenyu!</h1>
</body>
</html>

並在 controllers/welcome.php 中加載視圖

function saysomething($str) {
        $this->load->model('test_model');

        $info = $this->test_model->get_test_data();

        $this->load->view('test_view');
    }

4. 測試

訪問 http://localhost/learn-ci/index.php/welcome/hello ,可以看到如下輸出

Welcome, we finally met by MVC, my name is Zhangzhenyu!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM