学习yii2.0框架阅读代码(十八)

本文详细介绍了Yii2框架中模块的管理方式,包括模块唯一ID的获取、模块根目录的设置与获取、控制器路径的返回及视图文件目录的设置等关键功能。此外,还涉及了如何检查指定ID的子模块是否存在。

vendor/yiisoft/yii2/base/Module. php

    /**
     * 返回一个ID,惟一标识此模块在所有模块在当前应用程序.
     * @return string the unique ID of the module.
     */
    public function getUniqueId()
    {   //如果该模块是一个应用程序,将返回一个空字符串.
        return $this->module ? ltrim($this->module->getUniqueId() . '/' . $this->id, '/') : $this->id;
    }

    /**
     * Returns the root directory of the module.
     * It defaults to the directory containing the module class file.
     * @return string the root directory of the module.
     */
    public function getBasePath()
    {   //返回模块的根目录
        if ($this->_basePath === null) {
            $class = new \ReflectionClass($this);
            $this->_basePath = dirname($class->getFileName());
        }

        return $this->_basePath;
    }

    /**
     * 设置模块的根目录.
     * 这种方法只能在开始调用构造函数的使用
     * @param string $path the root directory of the module. This can be either a directory name or a path alias.
     * @throws InvalidParamException if the directory does not exist.
     */
    public function setBasePath($path)
    {
        $path = Yii::getAlias($path);
        // realpath — 返回规范化的绝对路径名
        $p = realpath($path);
        if ($p !== false && is_dir($p)) {
            // 路径存在,且是一个目录,就将它存到当前对象的$_basePath中
            $this->_basePath = $p;
        } else {
            throw new InvalidParamException("The directory does not exist: $path");
        }
    }

    /**
     * 返回目录包含控制器类根据[[controllerNamespace]]
     * @return string the directory that contains the controller classes.
     * @throws InvalidParamException if there is no alias defined for the root namespace of [[controllerNamespace]].
     */
    public function getControllerPath()
    {   //如果没有别名为的根名称空间就返回定义[[controllerNamespace]]。
        return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
    }


    /**
     * 集包含视图的目录文件.
     * @param string $path the root directory of view files.
     * @throws InvalidParamException if the directory is invalid
     */
    public function setViewPath($path)
    {
        $this->_viewPath = Yii::getAlias($path);
    }


    /**
     * 定义了路径别名.
     * 这个方法调用[[Yii:setAlias()]]注册路径别名.
     * 这种方法提供,这样您就可以定义路径别名配置模块.
     * @property array list of path aliases to be defined. The array keys are alias names
     * (must start with '@') and the array values are the corresponding paths or aliases.
     * See [[setAliases()]] for an example.
     * @param array $aliases list of path aliases to be defined. The array keys are alias names
     * (must start with '@') and the array values are the corresponding paths or aliases.
     * For example,
     *
     * ~~~
     * [
     *     '@models' => '@app/models', // an existing alias
     *     '@backend' => __DIR__ . '/../backend',  // a directory
     * ]
     * ~~~
     */
    public function setAliases($aliases)
    {
        foreach ($aliases as $name => $alias) {
            Yii::setAlias($name, $alias);
        }
    }

    /**
     * Checks whether the child module of the specified ID exists.
     * This method supports checking the existence of both child and grand child modules.
     * @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
     * @return boolean whether the named module exists. Both loaded and unloaded modules
     * are considered.
     */
    public function hasModule($id)
    {
        if (($pos = strpos($id, '/')) !== false) {
            // 检查是否指定的子模块ID存在
            $module = $this->getModule(substr($id, 0, $pos));

            return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
        } else {
            return isset($this->_modules[$id]);
        }
    }

 

转载于:https://www.cnblogs.com/xwzj/p/5453655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值