doupingyun73833 2013-10-24 13:37
浏览 12
已采纳

Cakephp静态数据收集

I was unsure about what to call this question but here is my problem:

I am creating a webshop in Cakephp. For this purpose i have alot of different categories that is bound to a static menu line.

Now regardless of where you are on the webshop you will be able to see these categories.

All the categories are stored in the database and then looped into the default layout (meaning that if i add a new category it will automaticly add it self to the menu line).

My question is since all of these categories has to be fetch no matter where you are in my application how do i collect them?

Only way i can think of is collecting them in every controller and set them to a $_SESSIONvariable and then check if the variable is set.

I know there must be another way but how?

Im using CakePHP 2.4

Also the menu of my application is stoped in Layouts->default.ctp

which means that it does not "care" which controller or action you are in. Meaning that the categories must be loaded before the actions

  • 写回答

2条回答 默认 最新

  • douzhi3105 2013-10-24 14:39
    关注

    Sound to me as if you are maybe looking for the beforeFilter() callback. Define it on your base controller (which is most likely AppController), and simply set the categories as a view variable, that way the data is available to all layouts on controllers that extend the base controller.

    Here's an abstract example of what it could look like:

    ...
    
    class AppController extends Controller
    {
        ...
    
        public $uses = array
        (
            'Category'
        );
    
        public function beforeFilter()
        {
            parent::beforeFilter();
    
            $this->set('categories', $this->Category->find('all'));
        }
    
        ...
    }
    

    In your layout you could then simply check for the existence of $categories and do whatever you have to do.

    if(isset($categories))
    {
        // show the category menu
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办