urlManager: using names instead of ID's

Hello folks,

I’m trying to use name’s instead of ID’s on URL’s.

I’ve changed everything else and things work through an URL like:

"http://domain.com/controller/view?name=myname"

But I’m trying to make it work simply through:

http://domain.com/controller/myname

  • NOTE 1. This changes the URL to a proper format, but I’m left with an error: “The system is unable to find the requested action ‘myname’”

'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName'=>false,

	'rules'=>array(


		'<controller:\w+>/<name:\d+>'=>'<controller>/view',   // *NOTE 1.

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

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

		

		/* Working version with the ID	

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

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

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

		*/

	),			

),

Can’t really figure out what I’m doing wrong here.

Do you have method actionMyname in ControllerController.php?

This works for me:




'urlManager'=>array(

        'urlFormat'=>'path',

        'showScriptName'=>false,

        'rules'=>array(

                '<controller:\w+>/<name:\w+>'=>'<controller>/view',   // changed <name:\d+> to <name:\w+>

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

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

        ),

),



This also works:




'urlManager'=>array(

        'urlFormat'=>'path',

        'showScriptName'=>false,

        'rules'=>array(

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

                array('artistProfile/view', 'pattern'=>'artist/profile/<name:\w+>'),

                array('musicGenre/view', 'pattern'=>'music/genre/<name:\w+>'),

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

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

        ),

),



By putting the rule above the default ‘<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’ rule

it will match my expression first. If it doesn’t match the expression, then it will try the default rules next. If the new rule comes after the default rule, the application will think it matches the default rule but it would actually return the error: “The system is unable to find the requested action ‘myname’”.

This also works:




'urlManager'=>array(

        'urlFormat'=>'path',

        'showScriptName'=>false,

        'rules'=>array(

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

                '<_c:(artistProfile|musicGenre)>/<name:\w+>/<_a:(view|create|update|delete)>'=>'<_c>/<_a>',

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

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

        ),

),