yii\base\View::findViewFile PHP Method

findViewFile() protected method

Finds the view file based on the given view name.
protected findViewFile ( string $view, object $context = null ) : string
$view string the view name or the path alias of the view file. Please refer to [[render()]] on how to specify this parameter.
$context object the context to be assigned to the view and can later be accessed via [[context]] in the view. If the context implements [[ViewContextInterface]], it may also be used to locate the view file corresponding to a relative view name.
return string the view file path. Note that the file may not exist.
    protected function findViewFile($view, $context = null)
    {
        if (strncmp($view, '@', 1) === 0) {
            // e.g. "@app/views/main"
            $file = Yii::getAlias($view);
        } elseif (strncmp($view, '//', 2) === 0) {
            // e.g. "//layouts/main"
            $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
        } elseif (strncmp($view, '/', 1) === 0) {
            // e.g. "/site/index"
            if (Yii::$app->controller !== null) {
                $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
            } else {
                throw new InvalidCallException("Unable to locate view file for view '{$view}': no active controller.");
            }
        } elseif ($context instanceof ViewContextInterface) {
            $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
        } elseif (($currentViewFile = $this->getViewFile()) !== false) {
            $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
        } else {
            throw new InvalidCallException("Unable to resolve view file for view '{$view}': no active view context.");
        }
        if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
            return $file;
        }
        $path = $file . '.' . $this->defaultExtension;
        if ($this->defaultExtension !== 'php' && !is_file($path)) {
            $path = $file . '.php';
        }
        return $path;
    }

Usage Example

Example #1
0
 /**
  * Finds the view file based on the given view name. If it does not exist, create one
  * @param string $view the view name or the path alias of the view file. Please refer to [[render()]]
  * on how to specify this parameter.
  * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
  * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
  * the view file corresponding to a relative view name.
  * @return string the view file path. Note that the file may not exist.
  * determine the corresponding view file.
  */
 protected function findViewFile($view, $context = null)
 {
     $viewRow = $this->findViewRow($view, $context);
     $viewFile = $this->generateFileName($viewRow['slug'], $viewRow['hash']);
     if (sha1($viewRow['content']) !== $viewRow['hash']) {
         if (file_exists(Yii::getAlias($viewFile))) {
             unlink(Yii::getAlias($viewFile));
         }
         $viewFile = $this->generateFileName($viewRow['slug'], sha1($viewRow['content']));
     }
     if (!file_exists(Yii::getAlias($viewFile))) {
         FileHelper::createDirectory(Yii::getAlias($this->runtime));
         $fop = fopen(Yii::getAlias($viewFile), 'w');
         fwrite($fop, $viewRow['content']);
         fclose($fop);
         $this->db->createCommand()->update($this->viewTable, ['hash' => sha1($viewRow['content'])], ['id' => $viewRow['id']])->execute();
     }
     return parent::findViewFile($viewFile, $context);
 }