备忘:yii2 中获取模块名 控制器名 方法名的方法

PHP 林涛 10397℃ 0评论
Yii2 获取模块名、控制器名、方法名
在视图中:
    模块名  $this->context->module->id
    控制器名 $this->context->id
    方法名 $this->context->action->id
    网址  Yii::$app->request->hostInfo
在控制器中
     模块名   Yii::$app->controller->module->id;
     控制器名   Yii::$app->controller->id
     方法名  Yii::$app->controller->action->id;
    模块名 $this->module->id;
    控制器名 $this->id;
     方法名  $this->action->id;
在控制器的 beforeAction 方法中(方法接收$action参数)
    模块名  $action->controller->module->id;
    控制器名 $action->controller->id;
    方法名  $action->id;
 ======================================
[Url::current] – 现在测试本地路径(http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user)
a: //获取当前路径 – 相对路径
$url = Url::current();
举例:/mobile/hmConnections/user/verify-user
b: //获取当前路径 – 相对路径
$url = Url::current([‘id’ => 1], false);
例如: /mobile/hmConnections/user/verify-user?id=1
c: //获取当前路径 – 绝对路径
$url = Url::current([‘id’ => 1], true);
例如: http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user?id=1
d: //获取当前路径 – 绝对路径 传输协议-http
$url = Url::current([‘id’ => 1], ‘http’);
例如: http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user?id=1
e: //获取当前路径 – 绝对路径 传输协议-https
$url = Url::current([‘id’ => 1], ‘https’);
例如: https://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user?id=1
[Url::toRoute] – 获取某一地址 => 现在测试本地路径(http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user)
1://获取某地址 – 相对路径
$url = Url::toRoute(‘site/index’);
例如: /mobile/hmConnections/site/index
2://获取某地址 – 相对路径
$url = Url::toRoute(‘site/index’, false);
例如: /mobile/hmConnections/site/index
说明: 等价于1 因为默认是false
3://获取某地址 – 相对路径
$url = Url::toRoute([‘site/index’, ‘id’ => 1]);
例如: /mobile/hmConnections/site/index?id=1
4://获取某地址的 – 绝对路径
$url = Url::toRoute(‘site/index’, true);
例如: http://daxia.dc.weixin.com/mobile/hmConnections/site/index
5://获取某地址的 – 绝对路径
$url = Url::toRoute(‘site/index’, [‘id’ => 1]);
例如: http://daxia.dc.weixin.com/mobile/hmConnections/site/index
说明: 参数没有输出,说明,这种写法[‘id’ => 1], 他当成了true,所以等价于4
6://获取某地址的 – 绝对路径 (传输协议-http)
$url = Url::toRoute(‘site/index’, ‘http’);
例如: https://daxia.dc.weixin.com/mobile/hmConnections/site/index
说明: 等价于4
7://获取某地址的 – 绝对路径 (传输协议-https)
$url = Url::toRoute(‘site/index’, ‘https’);
例如: https://daxia.dc.weixin.com/mobile/hmConnections/site/index
[Url::to] – 创建一个基于给定参数的网址 => 现在测试本地路径(http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user)
1): //获某网址 – 相对路径
$url = Url::to([‘site/index’]);
举例:/mobile/hmConnections/site/index
说明:等价于2
2): //获取网址(带参数) – 相对路径
$url = Url::to([‘site/index’, ‘id’ => 1]);
举例:/mobile/hmConnections/site/index?id=1
说明:等价于3
3): 获取当前路径 – 相对路径
$url = Url::to();
举例:/mobile/hmConnections/user/verify-user
等价于a
4): 获取url – 相对路径
$url = Url::to(‘@web/image/1.jpg’);
举例: /image/a.jpg
说明:它将指定到你的某一个别名目录下@web
5): 获取url – 相对路径
$url = Url::to(‘image/1.jpg’);
举例:image/a.jpg
6): 获取url – 绝对路径(@mobileUrl 自己配置好的)
$url = Url::to(‘@mobileUrl/image/1.jpg’, true);
举例:http://daxia.dc.weixin.com/static/mobile/image/1.jpg
7): //获取url – 绝对路径 (传输协议-https)
$url = Url::to(‘@mobileUrl/image/1.jpg’, ‘https’);
举例:https://daxia.dc.weixin.com/static/mobile/image/1.jpg
8): //获取url – 绝对路径 (传输协议-http)
$url = Url::to(‘@mobileUrl/image/1.jpg’, ‘http’);
举例:http://daxia.dc.weixin.com/static/mobile/image/1.jpg
说明:等价于 6)
** 特别说明下:@mobileUrl
$url = Url::to(‘@mobileUrl/city-partner/city-partner/images/1.png’);
– @mobileUrl,配置如下: Yii::setAlias(‘@mobileUrl’, Yii::getAlias(‘@web/static/mobile/’));
* 给Yii::getAlias(‘@web/static/mobile/’)定义一个别名@mobileUrl(
也就是,下次我们直接用@mobileUrl来表示Yii::getAlias(‘@web/static/mobile/’)的意思)
* @web指的是当前项目目录下的web下, 这是框架默认的
* 而当前目录也需要配置,一般是在common/config/bootstrap.php进行配置
配置如下:Yii::setAlias(‘service’, dirname(dirname(__DIR__)) . ‘/platform_service’);

如需转载请注明: 转载自26点的博客

本文链接地址: 备忘:yii2 中获取模块名 控制器名 方法名的方法

转载请注明:26点的博客 » 备忘:yii2 中获取模块名 控制器名 方法名的方法

喜欢 (0)
发表我的评论
取消评论

表情