yii\web\View::registerAssetBundle PHP Method

registerAssetBundle() public method

All dependent asset bundles will be registered.
public registerAssetBundle ( string $name, integer | null $position = null ) : AssetBundle
$name string the class name of the asset bundle (without the leading backslash)
$position integer | null if set, this forces a minimum position for javascript files. This will adjust depending assets javascript file position or fail if requirement can not be met. If this is null, asset bundles position settings will not be changed. See [[registerJsFile]] for more details on javascript position.
return AssetBundle the registered asset bundle instance
    public function registerAssetBundle($name, $position = null)
    {
        if (!isset($this->assetBundles[$name])) {
            $am = $this->getAssetManager();
            $bundle = $am->getBundle($name);
            $this->assetBundles[$name] = false;
            // register dependencies
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
            foreach ($bundle->depends as $dep) {
                $this->registerAssetBundle($dep, $pos);
            }
            $this->assetBundles[$name] = $bundle;
        } elseif ($this->assetBundles[$name] === false) {
            throw new InvalidConfigException("A circular dependency is detected for bundle '{$name}'.");
        } else {
            $bundle = $this->assetBundles[$name];
        }
        if ($position !== null) {
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
            if ($pos === null) {
                $bundle->jsOptions['position'] = $pos = $position;
            } elseif ($pos > $position) {
                throw new InvalidConfigException("An asset bundle that depends on '{$name}' has a higher javascript file position configured than '{$name}'.");
            }
            // update position for all dependencies
            foreach ($bundle->depends as $dep) {
                $this->registerAssetBundle($dep, $pos);
            }
        }
        return $bundle;
    }

Usage Example

Example #1
0
 /**
  * Registers this asset bundle with a view.
  * @param View $view the view to be registered with
  * @return static the registered asset bundle instance
  */
 public static function register($view)
 {
     return $view->registerAssetBundle(get_called_class());
 }
All Usage Examples Of yii\web\View::registerAssetBundle