Yii Framework Forum: embed menu from external file - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

embed menu from external file Rate Topic: -----

#1 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 04 May 2011 - 09:57 PM

hi there,
i wanna ask, i have problem to embed menu from external file
/protected/modules/site/components/Menu.php
return array(
				array('label'=>'Home', 'url'=>array('/site/index')),
				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
				array('label'=>'Contact', 'url'=>array('/site/contact')),
				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)
			);


i wanna call this file into CMenu widget on current themes. Can anybody tell me how to do that. Thx
xent

My first app: School Information System
Find error? Post here
My Blog
0

#2 User is offline   itmagetan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-February 11

Posted 04 May 2011 - 11:02 PM

It's easier to put your menu file in one location with your theme, then just include it
	
<div id="mainmenu"> 		
<?php include('Menu.php'); ?> 	
</div><!-- mainmenu -->

0

#3 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 05 May 2011 - 08:18 AM

thx for reply itmagetan,
if i use ur technique, its work. But i wanna little use oop technique, so change the Menu.php
class Menu {
    public function  getList() {
        return array(
            array('label'=>'Home', 'url'=>array('/site/index')),
            array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
            array('label'=>'Contact', 'url'=>array('/site/contact')),
            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)
	);
   }
}


on modules/site/SiteModule.php, i modify
class SiteModule extends CWebModule
{
    public $navmenu;
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(
			'Site.models.*',
			'Site.components.*',
		));
	
                
        }

public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
                        /*print_r(Yii::import('kio'));

                    exit;*/
                 $_menu = new Menu;
                 //print_r($menu->getList());exit;
                 $this->navmenu = $_menu->getList();
                 //print_r($this->menu);
			return true;
		}
		else
			return false;
	}
}


in current theme i call menu function
<?php $this->widget('zii.widgets.CMenu',array(
			'items'=>  $this->navmenu,
		)); ?>

its return nothing, but i took a test call this variable on sitemodule, and it work.
the problem just came when call in current theme (the value of $this->menu returns nothing). can anyone want to help me

This post has been edited by xent: 06 May 2011 - 02:04 AM

xent

My first app: School Information System
Find error? Post here
My Blog
0

#4 User is offline   itmagetan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-February 11

Posted 06 May 2011 - 01:39 AM

Have you already put this line in your main.php file?
	// autoloading model and component classes
	'import'=>array(
                ...
		'application.modules.site.components.*',
		...
	),

0

#5 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 06 May 2011 - 02:10 AM

i have followed your instruction but still not work, the value is still empty. I have added a complete code of siteModule (on my code above at reply no 2). I still confused about this. o yea, did i put those code in wrong place?
xent

My first app: School Information System
Find error? Post here
My Blog
0

#6 User is offline   itmagetan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-February 11

Posted 06 May 2011 - 02:22 AM

and in init function? Maybe it should be like this (i don't know if it will work or not):
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(
                        'Site.models.*',
                        'Site.components.*',
                        'application.modules.site.components.*',
                ));
        
                
        }

0

#7 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 07 May 2011 - 08:30 AM

thx for replay again :)
yeah i knew the problem. the variable is not set into defaultController of current module.
after I moved that code into controller, menu is generated successfully. But every controller of current module must has this code. Can we write this code in that siteModule and call in controller, in order not to write much code :P
xent

My first app: School Information System
Find error? Post here
My Blog
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users