CodeIgniter 源码解读之视图

视图

控制器及模型都讲完了,这篇,顺理成章的可以讲讲视图类,从此,MVC三剑客,都凑齐了。
接着模型中提到的例子,我在控制器里调用了视图 view 方法,并在 application/views 目录中新建了对应的视图文件。
我们现在研究一下视图加载的原理:

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

class Test extends CI_Controller {
	
	public function __construct() {

		parent::__construct();
		$this->load->model('Test_model');
	}

	public function index()
	{
		$data['title'] = $this->Test_model->list();
		# 调用load 中的 view 方法,并将 $data 数组传递给他
		$this->load->view('test_view', $data);
	}
}

现在将目光转向 Loader 中的view方法:

# 这里的 view 方法很简单
public function view($view, $vars = array(), $return = FALSE)
{
	# 这行语句里,调用了 _ci_prepare_view_vars 方法,go~ 走起看源码
	return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
}
protected function _ci_prepare_view_vars($vars)
{
	# 由于视图传值需要传一个数组
	# 所以这里首先判断了是否是数组
	if ( ! is_array($vars))
	{
		# 接着判断是否是对象
		# 是,则使用 get_object_vars 将对象属性组成的关联数组(PHP Manual)
		# 否,则返回一个空数组
		# 所以当我们给视图传递非数组及对象时,传递到视图中的值,则为 array()
		$vars = is_object($vars)
			? get_object_vars($vars)
			: array();
	}
	
	# 循环关联数组的键
	foreach (array_keys($vars) as $key)
	{
		# 如果数组的键是以 _ci_ 开头的,则删除掉
		if (strncmp($key, '_ci_', 4) === 0)
		{
			unset($vars[$key]);
		}
	}
	
	# 返回数组
	return $vars;
}

又回到 view 方法中,我们看到这里调用了 _ci_load 方法(代码量较多,我们细细品~~):

	protected function _ci_load($_ci_data)
	{
		// 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]) ? $_ci_data[$_ci_val] : FALSE;
		}
	
		$file_exists = FALSE;

		// Set the path to the requested file
		# 这里的 $_ci_path 为空
		if (is_string($_ci_path) && $_ci_path !== '')
		{
			# 按路径 / 分割
			$_ci_x = explode('/', $_ci_path);
			# 获取最后一个数组的值(就是获取视图的文件名)
			$_ci_file = end($_ci_x);
		}
		else
		{
			# 获取视图文件扩展名
			$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
			# 因为我们一般在调用视图的时候只写视图的文件名不带扩展名,这里给我们加上了
			$_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
			# 循环获取视图文件是否存在
			foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
			{
				if (file_exists($_ci_view_file.$_ci_file))
				{
					$_ci_path = $_ci_view_file.$_ci_file;
					$file_exists = TRUE;
					break;
				}

				if ( ! $cascade)
				{
					break;
				}
			}
		}
		
		# 如果视图文件不存在则抛出异常
		if ( ! $file_exists && ! 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.
		$_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.
		 */
		 # 拼装视图数据
		empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
		# 重组数组,将键值数组打散成 $key 访问形式
		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.
		# 兼容处理
		if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
		{
			echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
		}
		else
		{
			# 输出视图到浏览器
			include($_ci_path); // include() vs include_once() allows for multiple views with the same name
		}

		log_message('info', '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!
		 */
		 # 如果当前缓冲层级 大于 ci 的缓冲层级
		if (ob_get_level() > $this->_ci_ob_level + 1)
		{
			# 清除当前缓冲区,返回到上一层缓冲区
			# 缓冲区类似 栈 结构,先进后出
			ob_end_flush();
		}
		else
		{
			# 将当前输出了内容 赋值 给 Output 核心类的 $final_output
			$_ci_CI->output->append_output(ob_get_contents());
			# 清空缓存
			@ob_end_clean();
		}

		return $this;
	}

视图的加载机制已经看完罗,到这里,CI的 MVC的源码已经看完了,后期,我会继续找点其他的源码再一起看下,学习下,最近,找工作的事也挺烦了,哎~~,好吧,下期再见!!

发布了8 篇原创文章 · 获赞 0 · 访问量 165

猜你喜欢

转载自blog.csdn.net/weixin_43950095/article/details/104617795