Question

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

Show Category Listing in Home Page via PagesController in Cake PHP

But In View file i m getting below error (In 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'));
Was it helpful?

Solution

EDITED IN RESPONSE TO FULL PagesController:

I don't think your find and set are being executed (until after the view is rendered) as they are after the call to $this->render. Try moving them like this:

    $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();
    }

EDITED IN RESPONSE TO -1: Please show us your whole PagesController.php so we can see if there are any problems getting the data through to home.ctp. You have already established that the data are there before the $this->set in your controller but that $categories is not in fact set in the view ('Undefined variable') so this would seem a logical thing to explore next.

I appreciate this is not an 'Answer' but I am not allowed to 'Comment' on the original post so this is the only way I can communicate my request.

ORIGINAL RESPONSE: Can't comment because not enough reputation but could you show us a bit more of your Pages controller as I wonder if the data are not actually getting through to your view because of some problem there..

OTHER TIPS

because the render call is before your set statement

in other words the page is rendered before you set the categories

move your code at the beginning of the action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top