0

I'm trying to return a navigation menu using Yii PHP framework, but my controller is only outputting the first item in the array, here's my code. Note that this pattern isn't using the traditional MVC, the model i'm asking data for is being displayed site-wide, not directly to its's controller->view.

Model - get data;

//output pages for getPagesMenuItems() in base controller
        public function getAllPages(){
            $criteria = new CDbCriteria();
            $criteria->condition = "visible = 1";
            return Pages::model()->findAll($criteria);
        }

Base controller in components

public $pagesMenuItems = array();
$this->pagesMenuItems = $this->getPagesMenuItems();

protected function getPagesMenuItems() {
        //Non admin users - links to pages
        if (Yii::app()->user->isGuest){

            $rows = Pages::getAllPages();

            foreach($rows as $row) {
            return    array(
                    //$row->id , $row->title , $row->guid , $row->visible
                    array('label' => $row->title, 'icon' => 'fa fa-times', 'url' => array('/admin/pages/view/id/' . $row->id)),
                    '---',
                );

            }
//            return array();
        }
        else {}
    }

And this is the view in the main.php

$this->widget('booster.widgets.TbMenu', array(  
                'items' => $this->pagesMenuItems,
                'id' => 'pagesNav'
            ));

I know the issue is packaging the array in the foreach loop, as i've tested the output of the model and all data is correct

Can anyone see where i'm going wrong in my controller?

Thanks

1 Answer 1

3

change getPagesMenuItems function as below:

protected function getPagesMenuItems() {
    //Non admin users - links to pages
    $data = array();
    if (Yii::app()->user->isGuest){

        $rows = Pages::getAllPages();

        foreach($rows as $row) {
            $data[] = array('label' => $row->title, 'icon' => 'fa fa-times', 'url' => array('/admin/pages/view/id/' . $row->id));

        }
    }
    else {}
    return $data;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats fantastic mate, works a treat. i knew the issue was returning the array, just couldn't get syntax right. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.