如何创建动态导航菜单cakephp [英] how to create dynamic navigation menu cakephp

查看:204
本文介绍了如何创建动态导航菜单cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据类别和子类别制作导航菜单,例如:



http:// mysite / categories / view / 1/2



我试过菜单生成器1.01,但它只适用于acl用户。
我也阅读了这篇教程,但它

解决方案

您的第一个链接无效。



<坦率地说,我甚至不确定我是否明白你在做什么。您提供的第二个链接 - 我承认我只看第一个代码片段 - 不使用数据库表。如果你希望你的菜单是动态的,如在基于控制器的行动,这是有道理的。如果你希望它是动态的,因为在动态控制菜单项,你希望能够有CRUD访问(你知道CRUD代表什么,是吗?),这是一个不同的(真的很简单)的故事。



这是最基本的,可以想象的裸体例子。我在十五分钟内在我的沙盒蛋糕1.3.12安装一起打在一起。我很确定我得到了所有的名字改变了蛋糕2惯例,但一些调整可能是必要的。我试图把它削减到绝对的基础,并保持代码尽可能清晰。建立它,因为它适合你。


  1. 创建一个名为<$ c的表格$ c>菜单。

  2. 创建一个名为 menu.php 的模型。 $ b
  3. 创建 / Views / Menus c。

  4. 创建 MenusController.php 目录和 / views / Elements / Menus



    CREATE TABLE 菜单
    id int(11)unsigned NOT NULL auto_increment,
    varchar(255)NOT NULL default'',
    controller varchar(255)NOT NULL,
    action varchar(255)NOT NULL,
    created datetime NOT NULL,
    datetime default NULL,
    PRIMARY KEY( id
    )ENGINE = InnoDB DEFAULT CHARSET = utf8;


型号代码 - Menu.php:

 < ;? 
class Menu extends AppModel {
var $ name ='Menu';
}

控制器代码 - MenusController.php:

 < ;? 
class MenusController extends AppController {
var $ name ='Menus';

function index(){
if(isset($ this-> params ['requested'])&& $ this-> params ['requested'] == true){
$ menus = $ this-> Menu-> find('all');
return $ menus;
} else {
$ this-> set('menus',$ this-> Menu-> find('all'));
}
}

function add(){
if(!empty($ this-> data)){
if($ this- >菜单 - >保存($ this->数据)){
$ this-> Session-> setFlash(__('菜单项已保存',true)
}
}
}

//根据需要构建额外的CRUD功能,
//例如编辑/查看/删除。

}


$ b $ p每个MenusController操作都需要一个



因此,您可以在 / view / menus / /views/menus/index.ctp 和 /views/menus/add.ctp 。您在/views/menus/index.ctp中输入的内容甚至不重要,/views/menus/add.ctp是您将根据需要添加新菜单项的形式:

 < div class =menus form> 
<?php echo $ this-> Form-> create('Menu');?>
< fieldset>
< legend><?php __('添加菜单项'); ?>< / legend>
<?php
echo $ this-> Form-> input('name')
echo $ this-> Form-> input('controller');
echo $ this-> Form-> input('action');
?>
< / fieldset>
<?php echo $ this-> Form-> end(__('Submit',true));?>
< / div>

现在,创建 /views/elements/menus/main.ctp

 < ul class =header main-menu> 
< ;? if(!isset($ menus)|| empty($ menus)):
$ menus = $ this-> requestAction('/ menus / index');
endif;
foreach($ menus as $ menu):
?>
< li>
<?=< a href ='。DS。$ menu ['Menu'] ['controller']。DS。$ menu ['Menu'] ['action']。 ;。$ menu ['Menu'] ['name']。< / a>; ?>
< / li>
< ;? endforeach; ?>
< / ul>

最后,将元素 >因此它出现在每一页,不管视图。如果你使用默认的蛋糕布局,这意味着你应该编辑 /views/layouts/default.ctp 。你可能想把这个项目放在合理的地方,例如< div id =header> 元素。插入元素()调用如下:

 < div id =header> 
<! - 无论标记,代码等,你可能在标题是好的。 - >
<?= $ this->元素('menus / main'); ?>
< / div>
<! - ...其余的布局继续... - >

可能需要向元素添加样式以使其可读。根据需要为样式表添加样式(默认为cake.generic.css)。



HTH。 食谱是您的朋友。


How can I build navigation menu from categories and sub categories, like this:

http://mysite/categories/view/1/2

I tried menu builder 1.01 but it only works for acl users. I also read this tutorial, but it was not helpful.

解决方案

Your first link doesn't work.

Frankly, I'm not even sure I understand what you're trying to do. The second link you supplied - I admit I only looked at the first code snippet - doesn't use a database table. If you want your menu to be dynamic, as in based off controller actions, that makes sense. If you want it to be "dynamic" as in having dynamic control over menu items that you want to be able to have CRUD access to (you do know what CRUD stands for, right?), that's a different (really really simple) story.

This is the most basic, bare-bones example imaginable. I literally slapped this together in fifteen minutes in my sandbox Cake 1.3.12 installation. I am pretty sure I got all the names changed for Cake 2 conventions, however some tweaking may be necessary. I tried to pare it down to the absolute basics and keep the code as clear as possible. Build it out as it suits you. Refer to the cookbook and remember to follow Cake convention at all times.

  1. Create a table with a name like menus.
  2. Create a model called menu.php.
  3. Create a MenusController.php.
  4. Create the /Views/Menus directory, and /views/Elements/Menus.

    CREATE TABLE menus ( id int(11) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL default '', controller varchar(255) NOT NULL, action varchar(255) NOT NULL, created datetime NOT NULL, modified datetime default NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Model code - Menu.php:

<?
class Menu extends AppModel {
    var $name = 'Menu';
}

Controller code - MenusController.php:

<?
class MenusController extends AppController {
var $name = 'Menus';

function index() {
    if (isset($this->params['requested']) && $this->params['requested'] == true) {
        $menus = $this->Menu->find('all');
        return $menus;
    } else {
        $this->set('menus', $this->Menu->find('all'));
    }
}

function add() {
    if (!empty($this->data)) {
        if ($this->Menu->save($this->data)) {
            $this->Session->setFlash(__('The menu item has been saved', true));
        }
    }
}

    // Build out additional CRUD functionality, 
    // for example edit / view / delete, as desired.

}

Every MenusController action requires an [action name].ctp file in /view/menus/.

So create /views/menus/index.ctp and /views/menus/add.ctp. What you put in /views/menus/index.ctp isn't even terribly important, and /views/menus/add.ctp is the form you will use to add new menu items as desired:

<div class="menus form">
<?php echo $this->Form->create('Menu');?>
    <fieldset>
        <legend><?php __('Add Menu Item'); ?></legend>
<?php
    echo $this->Form->input('name');
    echo $this->Form->input('controller');
    echo $this->Form->input('action');
?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit', true));?>
</div> 

Now, create /views/elements/menus/main.ctp:

<ul class="header main-menu">
<? if (!isset($menus) || empty($menus)) :
        $menus = $this->requestAction('/menus/index');
    endif; 
    foreach($menus as $menu) : 
?>
    <li>
    <?="<a href='".DS.$menu['Menu']['controller'].DS.$menu['Menu']['action']."'>".$menu['Menu']['name']."</a>"; ?>
    </li>
<? endforeach; ?>
</ul>

Finally, place the element in the layout so it appears on every page, regardless of view. If you are using the default cake layout, that means you should edit /views/layouts/default.ctp. You probably want to put this item someplace sensible, such as the <div id="header"> element. Insert the element() call as follows:

<div id="header">
    <!-- whatever markup, code, etc. you may have in the header is fine. -->
    <?= $this->element('menus/main'); ?>
</div>
<!-- ...the rest of your layout continues... -->

It may be necessary to add styling to your element to make it readable. Add styling to whatever stylesheet/s you may have (the default is cake.generic.css) as desired.

HTH. The cookbook is your friend.

这篇关于如何创建动态导航菜单cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆