通过Yii2中的自定义控制器为特定用户启用调试器工具
为了启用调试器工具,我们修改了web/index.php文件通过Yii2中的自定义控制器为特定用户启用调试器工具,yii2,yii2-basic-app,Yii2,Yii2 Basic App,为了启用调试器工具,我们修改了web/index.php文件 defined('YII_DEBUG') or define('YII_DEBUG', false); defined('YII_ENV') or define('YII_ENV', 'prod'); 如何在自定义控制器中重写或更改这些变量 class CommonController extends Controller { public function init() { $user_id = 1; if($
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
如何在自定义控制器中重写或更改这些变量
class CommonController extends Controller {
public function init() {
$user_id = 1;
if($user_id == 1){
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
}
}
}
目标是为特定用户启用调试器。我知道有一种方法可以通过允许的IP
。但是,我正在寻找特定的用户智能。可能吗
基于此,
在某处创建一个新类,我使用了common\components\Debug
。从yii\debug\Module
扩展它,并用所需的逻辑覆盖checkAccess()
<?php
namespace common\components;
class Debug extends \yii\debug\Module
{
private $_basePath;
protected function checkAccess()
{
$user = \Yii::$app->getUser();
if (
$user->identity &&
$user->can('admin')
) {
return true;
}
//return parent::checkAccess();
}
/**
* @return string root directory of the module.
* @throws \ReflectionException
*/
public function getBasePath()
{
if ($this->_basePath === null) {
$class = new \ReflectionClass(new \yii\debug\Module('debug'));
$this->_basePath = dirname($class->getFileName());
}
return $this->_basePath;
}
}
当然,这将为所有用户运行调试器,但仅为管理员显示调试器。这可能会给应用程序带来一些不必要的开销,但调试过程会在检查标识之前开始。这些调试常量。。必须在应用程序运行之前声明。。所以问题是,你如何知道此时哪个用户正在运行应用程序。IP不在boostrap阶段,而是用户?是的。这就是问题所在@scaisEdge。我无法知道登录的用户id,因为这些常量是在应用程序运行之前定义的。有办法吗?。。可能正在使用会话设置用户,然后调用应用。。检查某些会话变量。。
if (!YII_ENV_TEST) {
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'common\components\Debug', // <--- Here
];
...
}