简体   繁体   中英

Yii one function in controler, and CMenu from Zii

I have one function that in my SiteController.php display data from a database based on the address. id=value .

public function actionIndex($id='index')
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'

        $this->pageTitle = 'Strona główna';
        $criteria  = new CDbCriteria(
            array(
                'condition' => 'name = :Name',
                'params' => array(':Name' => $id),
            )
        );
        $ModelPages = Pages::model()->findAll($criteria);
        $this->render('index',
                array(
                    'Model' => $ModelPages,
                )
            );
    }

How to improve the set up CMenu from Zii that accepts addresses. After entering the CMenu index.php?id=value or /index.php/id/value does not work. I would like to address in such a way that displayed the index.php/value . If I type in the address bar manually index.php?id=value it works.

<?php $this->widget('zii.widgets.CMenu',array(
            'items'=>array(
                array('label'=>'Home', 'url'=>array('index.php')),
                array('label'=>'O nas', 'url'=>array('/site/page', 'view'=>'about')),
                array('label'=>'Galeria', 'url'=>array('/site/contact')),
                array('label'=>'Kontakt', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
            ),
        )); ?>

You need to add a rule in your urlManager configuration. Something like this:

'<id:[^\/]*>' => 'site/index',

Put it above other rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM