一、getBasePath方法,返回模块的路径
/**
* 返回模块根目录
*/
public function getBasePath()
{
if ($this->_basePath === null) {
$class = new \ReflectionClass($this);
$this->_basePath = dirname($class->getFileName());
}
return $this->_basePath;
}
二、setBasePath方法,设置模块根目录
/**
* 设置模块根目录
*/
public function setBasePath($path)
{
//返回$path对应的真实路径
$path = Yii::getAlias($path);
$p = strncmp($path, 'phar://', 7) === 0 ? $path : realpath($path);
if (is_string($p) && is_dir($p)) {
$this->_basePath = $p;
} else {
throw new InvalidArgumentException("The directory does not exist: $path");
}
}
三、getControllerPath方法,返回控制器路径
//控制器路径
public function getControllerPath()
{
return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
}
四、getViewPath方法,返回视图目录
// 视图路径
public function getViewPath()
{
if ($this->_viewPath === null) {
$this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
}
return $this->_viewPath;
}
五、setViewPath方法,设置视图目录
// 设置视图路径
public function setViewPath($path)
{
$this->_viewPath = Yii::getAlias($path);
}
六、getLayoutPath方法,返回布局路径
// 布局路径
public function getLayoutPath()
{
if ($this->_layoutPath === null) {
$this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
}
return $this->_layoutPath;
}
七、setLayoutPath方法,设置布局
// 设置布局
public function setLayoutPath($path)
{
$this->_layoutPath = Yii::getAlias($path);
}
总结:
阅读了7个方法:
- getBasePath方法,返回模块的路径
- setBasePath方法,设置模块根目录
- getControllerPath方法,返回控制器路径
- getViewPath方法,返回视图目录
- setViewPath方法,设置视图目录
- getLayoutPath方法,返回布局路径
- setLayoutPath方法,设置布局