Create Own Urlmanager Rule

This is how I defined rule for my urls. I have controller named "vijesti" and parameter "naslov" to identify in database. And it works. when I open

www.domain.com/vijesti/9plkd6AKGF where "vijesti" is controller and "9plkd6AKGF" is "naslov" everything works just fine. But now other links wont open like /vijest/admin or /vijesti/create


   'urlManager'=>array(

    			'urlFormat'=>'path',

                'showScriptName'=>false,

    			'rules'=>array(

    												

    				'vijesti/<naslov:\w+>'=>'vijesti/view',

    

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

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

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

    			),

    		),



That’s because URL parser stops after first matching rule.

In your case vijesti/admin and vijesti/create are also intercepted by vijesti/<naslov:\w+>

You should define more specific regexp.

see this example


array(

    '<_c:(post|comment)>/<id:\d+>/<_a:(create|update|delete)>' => '<_c>/<_a>',

    '<_c:(post|comment)>/<id:\d+>' => '<_c>/read',

    '<_c:(post|comment)>s' => '<_c>/list',

)

from here

This works, but why


'<controller:\w+>/<naslov:([a-zA-Z0-9]{10})>'=>'<controller>/view',

That’s not reliable at all. Too many route will satisfy this rule.

You need something like


'vijesti/<_a:(admin|create|update|delete)>' => 'vijecti/<_a>',

'vijesti/<naslov:\w+>'=>'vijesti/view',



acutally I’ve added this


'<controller:(vijesti)>/<naslov:([a-zA-Z0-9]{10})>'=>'<controller>/view',

But now I understand little bit better from your example

Some things to remember when dealing with rules:

  1. Left side of the rule is just an old plain regexp.

So the rule '<controller:\w+>/<naslov:([a-zA-Z0-9]{10})> will result in regexp #(\w+)/([a-zA-Z0-9]{10})# which obviously matches many urls.

  1. Rules are tested one by one, starting from the top.

  2. Url parser stops when it finds the matching rule.

  3. Named params (<naslov:…> and so on) are taken into account only when creating the URL.

I noticed now that none of my other controller works except "vijesti".

So I had to change my rules and instead of


'vijesti/<naslov:\w+>'=>'vijesti/view',

write


'/some_string/<naslov:\w+>' => >'vijesti/view'

That is because now yii knows to always look for “some_string” first and then “naslov” parameter when vijest/view is opened. Before it was always first checking for parameter “naslov” whenever I opened any other view or controller so it didn’t work