AssetModel::viewLocation PHP Method

viewLocation() public static method

Get the path to a view.
public static viewLocation ( string $view, string $controller, string $folder, array | null $extensions = null ) : string | false
$view string the name of the view.
$controller string the name of the controller invoking the view or blank.
$folder string the application folder or plugins/ folder.
$extensions array | null optional. list of extensions to allow
return string | false The path to the view or false if it wasn't found.
    public static function viewLocation($view, $controller, $folder, $extensions = null)
    {
        $paths = [];
        // If the first character is a forward slash, this is an absolute path
        if (strpos($view, '/') === 0) {
            // This is a path to the view from the root.
            $paths[] = $view;
        } else {
            $view = strtolower($view);
            // Trim "controller" from the end of controller name, if its there
            $controller = strtolower(stringEndsWith($controller, 'Controller', true, true));
            if ($controller) {
                $controller = '/' . $controller;
            }
            // Get list of permitted view extensions
            if (is_null($extensions)) {
                $extensions = AssetModel::viewExtensions();
            }
            // 1. Gather paths from the theme, if enabled
            if (Gdn::controller() instanceof Gdn_Controller) {
                $theme = Gdn::controller()->Theme;
                if ($theme) {
                    foreach ($extensions as $ext) {
                        $paths[] = PATH_THEMES . "/{$theme}/views{$controller}/{$view}.{$ext}";
                    }
                }
            }
            // 2a. Gather paths from the plugin, if the folder is a plugin folder
            if (stringBeginsWith($folder, 'plugins/')) {
                // This is a plugin view.
                foreach ($extensions as $ext) {
                    $paths[] = PATH_ROOT . "/{$folder}/views{$controller}/{$view}.{$ext}";
                }
                // 2b. Gather paths from the application as a fallback
            } else {
                // This is an application view.
                $folder = strtolower($folder);
                foreach ($extensions as $ext) {
                    $paths[] = PATH_APPLICATIONS . "/{$folder}/views{$controller}/{$view}.{$ext}";
                }
                if ($folder != 'dashboard' && stringEndsWith($view, '.master')) {
                    // This is a master view that can always fall back to the dashboard.
                    foreach ($extensions as $ext) {
                        $paths[] = PATH_APPLICATIONS . "/dashboard/views{$controller}/{$view}.{$ext}";
                    }
                }
            }
        }
        // Now let's search the paths for the view.
        foreach ($paths as $path) {
            if (file_exists($path)) {
                return $path;
            }
        }
        trace(['view' => $view, 'controller' => $controller, 'folder' => $folder], 'View');
        trace($paths, __METHOD__);
        return false;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param string $view The view name.
  * @param string $controllerName The controller name for the view.
  * @param string $applicationFolder The application folder for the view.
  * @return EmailTemplate $this The calling object.
  * @throws Exception
  */
 public function setView($view, $controllerName = 'email', $applicationFolder = 'dashboard')
 {
     $this->view = AssetModel::viewLocation($view, $controllerName, $applicationFolder);
     return $this;
 }