Back to LegacyFactory class

Method createView

public \Joomla\CMS\MVC\View\ViewInterface
createView
(mixed $name, mixed $prefix = '', mixed $type = '', array $config = [])
Method to load and return a view object.
Parameters
  • string $name The name of the view.
  • string $prefix Optional view prefix.
  • string $type Optional type of view.
  • array $config Optional configuration array for the view.
Returns
  • \Joomla\CMS\MVC\View\ViewInterface The view object
Since
  • 3.10.0
-
  • \Exception
Class: LegacyFactory
Project: Joomla

Method createView - Source code

/**
 * Method to load and return a view object.
 *
 * @param   string  $name    The name of the view.
 * @param   string  $prefix  Optional view prefix.
 * @param   string  $type    Optional type of view.
 * @param   array   $config  Optional configuration array for the view.
 *
 * @return  \Joomla\CMS\MVC\View\ViewInterface  The view object
 *
 * @since   3.10.0
 * @throws  \Exception
 */
public function createView($name, $prefix = '', $type = '', array $config = [])
{
    // Clean the view name
    $viewName = preg_replace('/[^A-Z0-9_]/i', '', $name);
    $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
    $viewType = preg_replace('/[^A-Z0-9_]/i', '', $type);
    // Build the view class name
    $viewClass = $classPrefix . $viewName;
    if (!class_exists($viewClass)) {
        $path = Path::find($config['paths'], BaseController::createFileName('view', array('name' => $viewName, 'type' => $viewType)));
        if (!$path) {
            return null;
        }
        \JLoader::register($viewClass, $path);
        if (!class_exists($viewClass)) {
            throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path), 500);
        }
    }
    return new $viewClass($config);
}