题
我正在尝试在模块中创建一个小部件,然后从模块的“外部”加载该小部件。更特别的是我正在使用其他人编写的用户模块。我不想有一个单独的页面来显示登录表单,因此我尝试制作一个coptlet/widget(混乱)显示登录表单。基本上,我将代码从登录器移动到该小部件。然后,我尝试通过
<?php $this->widget('user.components.LoginForm'); ?>
但是,我有一个错误
CWebApplication does not have a method named "encrypting".
在此行中的用户识别类中:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
发生这种情况,因为我基本上试图在应用程序的上下文而不是模块的上下文中执行此代码。因此,“ yii :: app() - >控制器 - >模块”的技巧实际上无法正常工作。
- 我究竟做错了什么:-
- 有更好的方法来实现这一目标吗? IE在其他页面中显示该登录表格,通常通过在用户模块(用户/登录)中访问登录控制器或窗口小部件是正确的方法来显示的?
谢谢。
解决方案
快速解决方案
好,所以我只是最终做了
Yii::app()->getModule('user')->encrypting($this->password)
代替
Yii::app()->controller->module->encrypting($this->password)
请注意,现在该模块 必须 在主配置中被称为“用户”,但我认为这允许更具灵活性。即,我们不义务仅在模块中使用模块功能。
关于在模块范围外显示小部件的其他洞察力
在玩更多之后,这就是我所做的。在usermodule.php中,我创建了一个方法
public static function id() {
return 'user';
}
然后到处我需要使用的模块
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
我不喜欢有很多与模块相关的导入:
'application.modules.user.models.*',
'application.modules.user.components.*',
因为我们已经在usermodule.php中有这些导入:
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'user.models.*',
'user.components.*',
));
}
因此,每当您知道某些功能将在模块之外使用时,确保将模块加载就很重要。例如,在我试图在一个模块控制器之一中显示的登录窗口小部件中,我有此代码行:
$model = new UserLogin;
但是,UserLogin是用户模块内部的一个模型,为了自动加载此模型,我们首先必须确保该模块已初始化:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
我希望如果您像我一样坚持整个模块概念,这将有所帮助。http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/ 很有用,但很难找到=)
其他提示
您最好将加密()移动到扩展CuserIdentity的Myuseridentiy类中。无论您使用什么代码,他们都将方法放入控制器是一个坏主意,因此您无法重复使用该代码。
登录表单仍应发布到用户/登录控制器,但我想他们使用YII的标准登录代码,您可能需要对其进行修改以使用Myuserifentity。