With YII recently encountered. in the main.php template, main.php need to make a list into which all data of rows of a specific table should be unloaded. How do I get from the database and display them in principle, I already know, but how to generate such a menu? Taki, it seems to me, there is a special logical and correct option.

  • Links to articles and paragraphs in manuals are welcome (I did not find it myself), as well as general information on the menu in yii - heleg
  • I’ve done a shorter buildLeftMenu () method in Controller public that returns an array with the data for the CMenu widget. main.php just calls it inside the menu widget. is that good to do? - heleg

1 answer 1

There is such a widget in Yii, called CMenu: http://www.yiiframework.com/doc/api/1.1/CMenu

In the controller (for example, Controller) you create a public method that will return an array with the names of the items:

 public function getMenuItems() { $dbQuery = Menu::model()->findAll(); //Если используете ActiveRecord $menuItems = array(); foreach($dbQuery as $item) { $menuItems[] = array('label'=>$item->name, 'url'=>array('')); } return $menuItems; } 

In the layout, use the resulting array as a parameter in the widget:

 $this->widget('zii.widgets.CMenu', array( 'items'=>$this->getMenuItems(), )); 

This method can be pulled from the database and other data, such as url'y.

As I understand it, you made a sample on the same principle. But the details did not specify, so I painted the whole process.

A similar issue has been discussed here: http://www.yiiframework.com/forum/index.php/topic/30072-cmenu-%D1%84%D0Be%D1%80%B0 80% D0% BE% D0% B2% D0% B0% D1% 82% D1% 8C-% D0% B8% D0% B7-% D0% B1% D0% B4 /

  • Yes, I did just that. thanks - heleg