我正在尝试在模块中创建一个小部件,然后从模块的"外部"加载该小部件.更具体地说,我正在使用其他人编写的用户模块.我不想有一个单独的页面来显示登录表单,因此我尝试制作一个显示登录表单的CPortlet/widget(混淆).基本上,我已经将LoginController中的代码移动到该小部件中.然后我尝试在一些随机页面上显示小部件
<?php $this->widget('user.components.LoginForm'); ?>
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误
CWebApplication does not have a method named "encrypting".
Run Code Online (Sandbox Code Playgroud)
在此行的UserIdentity类中:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
Run Code Online (Sandbox Code Playgroud)
发生这种情况,因为我基本上试图在应用程序的上下文中执行此代码,而不是模块.因此,"Yii :: app() - > controller-> module"技巧并没有按预期工作.
谢谢.
好的,所以我结束了
Yii::app()->getModule('user')->encrypting($this->password)
Run Code Online (Sandbox Code Playgroud)
代替
Yii::app()->controller->module->encrypting($this->password)
Run Code Online (Sandbox Code Playgroud)
请注意,现在模块必须在主配置中被称为"user",但我认为这样可以提供更大的灵活性.即我们不一定只使用模块中的模块功能.
在玩了更多这就是我做的.在UserModule.php中,我创建了一个方法
public static function id() {
return 'user';
}
Run Code Online (Sandbox Code Playgroud)
然后到处都需要我使用的模块
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
Run Code Online (Sandbox Code Playgroud)
我不喜欢有很多与模块相关的导入,例如:
'application.modules.user.models.*',
'application.modules.user.components.*',
Run Code Online (Sandbox Code Playgroud)
因为我们已经在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.*',
));
}
Run Code Online (Sandbox Code Playgroud)
因此,每当您知道某些功能将在模块外部使用时,确保加载模块非常重要.例如,在我试图在其中一个模块控制器中显示的LoginForm小部件中,我有这行代码:
$model = new UserLogin;
Run Code Online (Sandbox Code Playgroud)
但是,UserLogin是User模块内部的模型,为了能够自动加载此模型,我们首先必须确保模块已初始化:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
Run Code Online (Sandbox Code Playgroud)
如果你像我一样坚持整个模块概念,我希望这会有所帮助. http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/很有用但很难找到=)