Yii Module code generation problems using Gii
I have just used Gii to generate a new module called gig. After generating the module code with Gii, I updated the config/main.php file to include the 'gig' module as follows:
'import'=>array(
'application.models.*',
'application.components.*',
'application.modules.*',
),
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'secretpassword',
),
'gig',
),
Now, when I try to access the auto-generated Yii module code in my browser as follows:
http://localhost/gig/default/index/
I receive the following error:
DefaultController cannot find the requested view "index".
To fix this problem, I changed the render code in the module's DefaultController.php from this:
$this->render('index');
to this:
$this->render('gig.views.default.index');
This change resolved the problem, but I am wondering if I have missed something or if Gii is generating buggy code for modules? Anybody experience this problem before? Is my solution correct?
Now that the controller can find the view, I reloaded the page, only to be shown this开发者_如何学JAVA error message:
Trying to get property of non-object
Turns out that there is a problem with the following code in the index.php view file:
$this->breadcrumbs=array(
$this->module->id,
);
I am not sure why this is happening. I tried changing the above code to the following:
$this->breadcrumbs=array(
Yii::app()->controller->module->id,
);
but this still gives me the same error message, "Trying to get property of non-object".
Any idea what may be wrong? Am I missing something when setting up the module code? I am using Yii 1.1.7
Thanks!
Sorry, it was our own stupid mistake. One of our programmers added a __construct method to the Controller class and forgot to include the $module variable in that method as follows:
public function __construct($id='site')
{
parent::__construct($id);
//custom code here
}
After adding $module variable as shown below, everything works fine now.
public function __construct($id='site', $module = null)
{
parent::__construct($id, $module);
}
If you don't need the breadcrumbs, simply delete that code block. If you do want them, make sure your Controller.php (which extends CController.php) class has:
public $breadcrumbs=array();
Then it should work as expected.
I'm not sure why you were having URL/path problems, but check your URL manager in config/main.php to see if there might be a rule that is causing problems. Seems to work for me without any rules, but if you do have some, try putting this first in the rules array:
'gig'=>'gig',
'gig/<controller:\w+>'=>'gig/<controller>',
'gig/<controller:\w+>/<action:\w+>'=>'gig/<controller>/<action>
精彩评论