标签:
View中的查找视图文件方法和渲染文件方法
1 /** 2 * Finds the view file based on the given view name. 3 * 通过view文件名查找view文件 4 * @param string $view the view name or the path alias of the view file. Please refer to [[render()]] 5 * on how to specify this parameter. 6 * @param string $view 视图文件名 7 * @param object $context the context to be assigned to the view and can later be accessed via [[context]] 8 * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate 9 * the view file corresponding to a relative view name. 10 * @param object $context 情景 11 * @return string the view file path. Note that the file may not exist. 12 * @return string view文件路径 13 * @throws InvalidCallException if a relative view name is given while there is no active context to 14 * determine the corresponding view file. 15 */ 16 protected function findViewFile($view, $context = null) 17 { 18 if (strncmp($view, ‘@‘, 1) === 0) {//判断$view是否是别名路径 19 // e.g. "@app/views/main" 20 $file = Yii::getAlias($view);//调用getAlias()方法获取真实路径 21 } elseif (strncmp($view, ‘//‘, 2) === 0) {//判断$view是否以‘//‘开始 22 // e.g. "//layouts/main" 23 //调用getViewPath()方法在当前模块下查找视图文件目录路径,然后拼接文件名合成视图文件路径 24 $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, ‘/‘); 25 } elseif (strncmp($view, ‘/‘, 1) === 0) {//判断$view是否以 ‘/‘开始 26 // e.g. "/site/index" 27 if (Yii::$app->controller !== null) {//且控制器存在 28 //则调用getViewPath()查找当前控制器对应的视图文件目录,然后拼接文件名合成视图文件路径 29 $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, ‘/‘); 30 } else {//否则,抛出异常 31 throw new InvalidCallException("Unable to locate view file for view ‘$view‘: no active controller."); 32 } 33 } elseif ($context instanceof ViewContextInterface) { 34 $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view; 35 } elseif (($currentViewFile = $this->getViewFile()) !== false) { 36 $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view; 37 } else { 38 throw new InvalidCallException("Unable to resolve view file for view ‘$view‘: no active view context."); 39 } 40 41 if (pathinfo($file, PATHINFO_EXTENSION) !== ‘‘) {//如果视图文件的扩展名不为空,直接返回扩展名 42 return $file; 43 } 44 $path = $file . ‘.‘ . $this->defaultExtension;//给视图文件添加扩展名 45 if ($this->defaultExtension !== ‘php‘ && !is_file($path)) {//这里应该是做二次校验,添加扩展名 46 $path = $file . ‘.php‘; 47 } 48 49 return $path; 50 } 51 52 /** 53 * Renders a view file. 54 * 渲染一个view文件 55 * 56 * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long 57 * as it is available. 58 * 如果[theme]可用(即非空),该方法将视图渲染themed version的视图文件直到[theme]不可用 59 * The method will call [[FileHelper::localize()]] to localize the view file. 60 * 该方法将调用[FileHelper::localize()]方法本地化视图文件 61 * 62 * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file. 63 * Otherwise, it will simply include the view file as a normal PHP file, capture its output and 64 * return it as a string. 65 * 如果[renderers|renderer]可用(即非空),该方法将渲染视图文件,否则将把视图文件作为常规的php文件包含进来 66 * 捕捉它的输出并将其返回为字符串 67 * 68 * @param string $viewFile the view file. This can be either an absolute file path or an alias of it. 69 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. 70 * @param object $context the context that the view should use for rendering the view. If null, 71 * existing [[context]] will be used. 72 * @return string the rendering result 73 * @throws InvalidParamException if the view file does not exist 74 */ 75 public function renderFile($viewFile, $params = [], $context = null) 76 { 77 $viewFile = Yii::getAlias($viewFile);//调用getAlias()方法处理输入的视图文件名 78 79 if ($this->theme !== null) { 80 $viewFile = $this->theme->applyTo($viewFile);//如果theme非空,怎将其应用到视图文件 81 } 82 if (is_file($viewFile)) { 83 $viewFile = FileHelper::localize($viewFile);//调用[FileHelper::localize()]方法本地化视图文件 84 } else {//$viewFile不是文件,抛出异常“视图文件不存在” 85 throw new InvalidParamException("The view file does not exist: $viewFile"); 86 } 87 88 $oldContext = $this->context; 89 if ($context !== null) { 90 $this->context = $context; 91 } 92 $output = ‘‘; 93 $this->_viewFiles[] = $viewFile; 94 95 if ($this->beforeRender($viewFile, $params)) {//如果renderFile的前置事件执行成功 96 Yii::trace("Rendering view file: $viewFile", __METHOD__);//记录trace信息 97 $ext = pathinfo($viewFile, PATHINFO_EXTENSION);//获取视图文件扩展名 98 if (isset($this->renderers[$ext])) {//判断视图文件的扩展名是否支持 99 if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) { 100 $this->renderers[$ext] = Yii::createObject($this->renderers[$ext]); 101 } 102 /* @var $renderer ViewRenderer */ 103 $renderer = $this->renderers[$ext];//如果支持的,将view渲染器对象赋值给$renderer 104 $output = $renderer->render($this, $viewFile, $params);//渲染视图文件 105 } else {//如果视图文件不是支持的类型,则以普通php文件处理 106 $output = $this->renderPhpFile($viewFile, $params); 107 } 108 $this->afterRender($viewFile, $params, $output);//调用renderFile的后置方法 109 } 110 111 array_pop($this->_viewFiles);//删除viewFiles中最后面的一个元素 112 $this->context = $oldContext; 113 114 return $output; 115 }
标签:
原文地址:http://www.cnblogs.com/isleep/p/5437312.html