urlManager

Hi everyone,

I’m new to this forum, and i have been studying yii for one week. Today i’m facing my first problem:

I’m trying to make URLs more SEO friendly, by writing some rules for urlManager; in particular, I created a CRUD using Gii, and wrote this rule in order to customize the URL of viewing action:




...

		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=> false,

			'urlSuffix'=>'.html',

			'caseSensitive' => false,

			'rules'=>array(				

				'site/pages/<view:\w+>'=>'site/page',

				'products/<category:\w+>'=>'products/index',

				'<id:\d+>-<product:\w+>'=>'products/view',

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

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

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

			),

		),

...



The URL for the ‘view’ action is generated by this code:




CHtml::link($html, array('view', 'id' => $data->id, 'product' => str_replace(' ', '-', $data->product)), array('class'=>'link'));



The thing is that the URL example/1-product-name.html generates this output, and i’m stuck…

Error 404

Unable to resolve the request "1-product-name".

Any help would be very appreciate.

Thank you very much.

Best regards

PS:i’m a non native english speaker…forgive me for my errors! :D

Welcome to the forum!

Use this rule:




'<id:\d+>-<product:[\w\-]+>'=>'products/view',



\w+ is only for letters ;)

Thank you! :D




'<id:\d+>-<product:[\w\-]+>'=>'products/view',



it works like a charm!!

So, \d+ stands for ‘digits’, \w+ is only for letters…but what’s the meaning of [\w\-]+?

Sorry for pointing you to a wiki page, but you definetely should start learning regular expressions now! :P Here is a link: http://en.wikipedia.org/wiki/Regular_expression

[\w\-]+ stands for "letter or -" 1 or more times.

P.S. Weee, my 1000th post here :D

Ah…i know regex, but I didn’t notice that they where regular expressions! O_O

Maybe I’d better go bed now! xD

Thank you very much

PS: 3 post! ;D