通过Cake PHP中的PagesController在主页中显示类别列表

时间:2014-02-17 12:33:57

标签: cakephp cakephp-2.3

PagesController.php

    $this->loadModel('Category');
            $categories = $this->Category->find('all',
                array(
                'limit' => 5,
                'order' => 'Category.id ASC',
                'fields' => array('Category.id','Category.category'),
                'contain' => false,
                'conditions' => array('Category.super_id' => 1),
                'recursive' => 0
                )
            );

        //pr($categories);  Here showing all category listing

        $this->set(compact('categories'));
       //$this->set('categories',$categories); that code also used

通过Cake PHP中的PagesController在主页中显示类别列表

但在View文件中我遇到错误(在视图/ Pages / home.ctp中)

 <?php pr($categories); ?>

 Notice (8): Undefined variable: categories [APP/View/Pages/home.ctp, line 142]

pagesController.php

    public function display() {
        $path = func_get_args();

        $count = count($path);
        if (!$count) {
            return $this->redirect('/');
        }
        $page = $subpage = $title_for_layout = null;

        if (!empty($path[0])) {
            $page = $path[0];
        }
        if (!empty($path[1])) {
            $subpage = $path[1];
        }
        if (!empty($path[$count - 1])) {
            $title_for_layout = Inflector::humanize($path[$count - 1]);
        }
        $this->set(compact('page', 'subpage', 'title_for_layout'));

        try {
            $this->render(implode('/', $path));
        } catch (MissingViewException $e) {
            if (Configure::read('debug')) {
                throw $e;
            }
            throw new NotFoundException();
        }

        $this->loadModel('Category');
        $categories = $this->Category->find('all',
            array(
            'order' => 'Category.id ASC',
            'fields' => array('Category.id','Category.category'),
            'contain' => false,
            'conditions' => array('Category.super_id' => 1),
            'recursive' => 0
            )
        );

        $this->set('categories',$categories);
        //$this->set(compact('categories'));
        //pr($categories);



    }

routes.php文件

 Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

2 个答案:

答案 0 :(得分:1)

编辑回应完整的PagesController:

我不认为您的查找和设置正在执行(直到呈现视图之后),因为它们是在调用$ this-&gt; render之后。尝试像这样移动它们:

    $this->loadModel('Category');
    $categories = $this->Category->find('all',
        array(
        'order' => 'Category.id ASC',
        'fields' => array('Category.id','Category.category'),
        'contain' => false,
        'conditions' => array('Category.super_id' => 1),
        'recursive' => 0
        )
    );

    $this->set('categories',$categories);        

    try {
        $this->render(implode('/', $path));
    } catch (MissingViewException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }

编辑回应-1: 请向我们展示您的整个PagesController.php,以便我们可以看到将数据传递到home.ctp是否有任何问题。您已经确定数据在控制器中设置$ this-&gt;之前已存在,但实际上并未在视图中设置$ categories('未定义变量'),因此下一步探讨这似乎是合乎逻辑的事情。 / p>

我很欣赏这不是'答案',但我不允许对原帖发表评论,所以这是我传达请求的唯一方式。

原始回复: 无法评论,因为没有足够的声誉,但你可以向我们展示一些你的页面控制器,因为我想知道数据是否因为某些问题而没有真正进入你的视图..

答案 1 :(得分:1)

因为render来电是在set陈述

之前

换句话说,在设置类别

之前会渲染页面

在操作开始时移动代码