简体   繁体   中英

Custom Widget in Yii

I create new widget like this:

class MyMenu extends CWidget
{
    public function init()
    {

    }

    public function run()
    {
        $Items = array(
                    array('label'=>'Home', 'url'=>array('/site/index')),
                    array('label'=>'Add User', 'url'=>array('/user/register')),
                    array('label'=>'View Users', 'url'=>array('/user/index')),
                    array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                    array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
                );
        $this->render('MyMenu', array('Items'=>$Items));
    }
}

and view for this widget

echo '<div id="Menu">';
$this->widget('zii.widgets.CMenu',array(
                'items' => $Items,
        ));
echo '</div>';

This is paths of my widget files:

在此处输入图片说明

But i get error

MyMenu Widget can not find the view "MyMenu"

What i made wrong?

You should call the directory views instead of view .

Source code shows it: http://www.yiiframework.com/doc/api/1.1/CWidget#getViewPath-detail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM