Yii 1.1: DB Driven CMenu (Widget)

1 follower

Awhile ago I was trying to find a database driven CMenu system to add to my web applications. This always seemed to me that it would be a lot easier to manage if it where in a database. So the following is the results of that effort:

Note: The CSS and styling is based on dynamicdrive jquery slide menu

Setup Steps:

1. Download widget

GIT Repository

Download DBMenu.zip from: DBMenu.zip

Extract DBMenu to your extensions folder: e.g. /protected/extensions

2. Create menu tables

Create the following tables in your database. Note: All SQL files have been provided in db folder of extension.

--
-- Table structure for table `menu`
--
 
CREATE TABLE IF NOT EXISTS `menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(128) NOT NULL,
  `view` varchar(128) NOT NULL,
  `url` varchar(128) NOT NULL,
  `linkOption` text NOT NULL,
  `visibility` int(11) NOT NULL,
  `role` varchar(77) NOT NULL,
  `position` int(11) NOT NULL,
  `menu_group_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=50 ;
 
 
 
 
INSERT INTO `menu` (`id`, `label`, `view`, `url`, `linkOption`, `visibility`, `role`, `position`, `menu_group_id`) VALUES
(0, 'Home', '/site/index', '/site/index', 'js:alert();', 1, 'domain_users', 1, 1),
(46, 'Menus', '', '#', '', 1, 'domain_users', 1, 2),
(48, 'Home', '#', '/site/index', 'js:alert($(''#username'').val());', 1, 'domain_users', 0, 2);
--
-- Table structure for table `menugroups`
--
 
CREATE TABLE IF NOT EXISTS `menugroups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(77) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Table structure for table `submenu`
--
 
CREATE TABLE IF NOT EXISTS `submenu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(70) NOT NULL,
  `view` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `linkOption` text NOT NULL,
  `position` int(11) NOT NULL,
  `visibility` int(11) NOT NULL,
  `menu_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;
 
INSERT INTO `submenu` (`id`, `label`, `view`, `url`, `linkOption`, `position`, `visibility`, `menu_id`) VALUES
(22, 'Submenus', '', '/submenu/admin', '', 2, 1, 46),
(23, 'Menus', '', '/menu/admin', '', 1, 1, 46);
--
-- Table structure for table `subsubmenu`
--
 
CREATE TABLE IF NOT EXISTS `subsubmenu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(70) NOT NULL,
  `view` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `linkOption` text NOT NULL,
  `position` int(11) NOT NULL,
  `visibility` int(11) NOT NULL,
  `sub_menu_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

3. Generate Models & CRUD for the menu tables

At this point you will want to generate model and crud via Gii for the following tables:

menu menugroups submenu subsubmenu

4. Add menu to /protected/views/layouts/main.php replacing CMenu widget.

Now the magic: Replace the default CMenu widget with this one: also make sure that you change the menu div to :

<div id="myslidemenu" class="jqueryslidemenu">
<?php $this->widget('ext.DBMenu.DBMenu',array(
           'group_id'=>$group,
 
 
               )); 
 ?>

Be the first person to leave a comment

Please to leave your comment.

Write new article
  • Written by: Preacher
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +1
  • Viewed: 2,900 times
  • Created on: May 29, 2014
  • Last updated: May 29, 2014
  • Tags: DB Driven CMenu