Rewrite Url Help

Hi everyone,

I’ve a little question for you.

I have mysite.com/cars page where I list all the cars. I’m using CListView widget in views/cars/index.php

What I would to do is change the pagination buttons, customizing rewrite rules.

Now I have on the bottom of the page mysite.com/cars?Cars_page=2, mysite.com/cars?Cars_page=3 and so on…

I would have something like mysite.com/cars/page/2 or mysite.com/cars?page=2

my rewrite rules on config are





		'urlManager'=>array(

			'urlFormat'=>'path',

                        'showScriptName'=>false,

                        'caseSensitive'=>false,

			'rules'=>array(

                                '' => 'site/index',

                                

                                'cars/' => 'cars/index',

                                'cars/<name>/<id:\d+>/' => 'cars/view',

                                

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>/<id:\d+>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),



view/cars/index.php




$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'ajaxUpdate'=>false,

	'summaryText'=>'Results: {start}-{end} di {count}', 	

        'pager'=> array(

		'header'=>'',

		'footer'=>'',

		'htmlOptions' => array('class'=>'pagination pagination-lg'),

		'prevPageLabel'  => '<',

		'nextPageLabel'  => '>',

		'firstPageLabel' => '&laquo;',

		'lastPageLabel'  => '&raquo;',

		'selectedPageCssClass' => 'active',

		'maxButtonCount' => 10

	),	

));



Anyone can help me?

Thanks

Hi pippo,

for mysite.com/cars/page/2 configure your url manager like this:


'urlManager'=>array(

    'rules'=>array(

        'cars/page/<page:\d+>' => 'cars/index',

    ),

),

or by echoing this:


echo $this->createUrl('cars/index', array(

        'page' => '2',

));

it gets you mysite.com/cars?page=2

syl s.

Ohh thanks.

But it doesn’t work. mysite.com/cars/page/x or mysite.com/cars?page=x always show the first page.

I don’t understand…

You should set pageVar to "page" in CActiveDataProvider pagination config.

Ohh yes !!

Thanks a lot

Now i’m very happy!

The last question (if possible)

Now in my pagination bar I have /cars?page=2, /cars?page=3, etc… and it works fine!

If I set on the url /cars/page/2, /cars/page/3 works fine too.

By setting this on the Model




	public function search()

	{

		$criteria=new CDbCriteria;

		if ((isset($_GET['search']) && ($_GET['search']!=''))) {

			$criteria->condition = ' name LIKE "%'.$_GET['search'].'%"';

		} else {

			$criteria->condition = '';

		}

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination' => array(

				'pageSize' => 25,

				'pageVar'=>'page'

			),

			'sort'=>array('defaultOrder'=>'name ASC')

		));

	}



I get pagination links like /cars?page=x

How can I get links like /cars/page/x

Thanks again!!!

You get the query string because you have no url rule to match your pagination.

A url manager rule like




'cars/page/<page:\d+>' => 'cars/index'



should do it.

Also, your query is vulnerable to sql injection. You should never query like this.

Change it to:




$criteria=new CDbCriteria;

$criteria->compare('name', (isset($_GET['search']) ? $_GET['search'] : null), true);



I have this in my rules (config/main.php). Have I to change my CActiveDataProvider pagination options? And how?

Thanks! Just do it

Can you show all url rules? Maybe they are in wrong order…





		'urlManager'=>array(

			'urlFormat'=>'path',

                        'showScriptName'=>false,

                        'caseSensitive'=>false,

			'rules'=>array(

                                '' => 'site/index',

                                

                                'cars/' => 'cars/index',

                                'cars/page/<page:\d+>' => 'cars/index',

                                'cars/<name>/<id:\d+>/' => 'cars/view',


...

...

...

                                                             

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>/<id:\d+>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),




You don’t need this:




'cars/' => 'cars/index',



Take it out and it will work.

Also, the rule




'cars/<name>/<id:\d+>/' => 'cars/view',



Should be something like:




'cars/<name:\w+>/<id:\d+>/' => 'cars/view',



Thanks !!! Great !!!

Is Yii the best PHP Framework?

if you know how to use it, Yes It Is :)

Yes it is is the answer! Always :D

What about the following function ?




	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		if ((isset($_GET['search']) && ($_GET['search']!=''))) {

			$words = explode(" ", $_GET['search']);

			$condition_string = '1';

			foreach ($words as $word) {

				$condition_string.=' AND (CONCAT(name,surname) LIKE "%'.$word.'%")';

			}

			$criteria->condition = $condition_string;

		} else {

			$criteria->condition = '';

		}

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination' => array(

					'pageSize' => 20,

					'pageVar' => 'page'

				),

			'sort'=>array('defaultOrder'=>'id DESC')

		));		

	}



Maybe Sql injection here isn’t possible (cause the explode) but there is a chance to do it with compare methods?

Thanks again

Nope, sql injection is possible in your case.

The correct approach would be




$criteria=new CDbCriteria;

                if (!empty($_GET['search'])) {

                        $words = explode(" ", $_GET['search']);

                        foreach ($words as $word) {

                                $criteria->addSearchCondition('CONCAT(name,surname)', $word, true, 'OR');

                        }

                }



Please see http://www.yiiframework.com/doc/api/1.1/CDbCriteria

Also, never use a php variable directly in a SQL string, if you must, use placeholders and bind params.

Please do your homework about using Yii’s AR :)