writing fancy URL translation rules

good evening everyone,

i admit yii is a good framework but the lack of well written manual can keep it from spreading.

url rules designing in CUrlManger is a fancy that require a lot imagination to master.

now i need my route to work like this:

controller/abc/xyz/123 should be interpreted as controller/actionIndex/?target=/abc/xyz/123

but

controller/create and controller/update should be interpreted as controller/actionCreate and controller/actionUpdate

how should i design the url translation rules?

thank you so much in advance

Not sure how you would solve the first example, I’m still learning myself this myself.

But let’s assume you have an advanced pattern/rule in place, set to solve your first routing. Then to solve the other ‘problem’ it’s really easy.

The URL is parsed through the rules until it comes across the first one that matches. So, it would something like this:




'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName' => false,

	'rules'=>array(

		'controller/create'=>'controller/create',

		'controller/update'=>'controller/update',

		'advanced rule'=>'that I dont know how to set',

	),

),



That way, when you supply an URL like serverdomain/controller/create it will match the first rule. Second with update, and the more advanced rule down there at third place wont kick in.

Now all you have to do is figure out the advanced rule. Good luck!

Edit:

Ok so I’ve toyed around with this for exercising reasons, and I’ve found one pattern that works like you want. I have no idea why you’d want it set up like that though, I’ll give you that :)




'urlManager'=>array(

	'urlFormat'=>'path',

	'showScriptName' => false,

	'rules'=>array(

		'controller/create'=>'controller/create',

		'controller/update'=>'controller/update',

		'controller/*<target:\/\w+\/\w+\/\d+>'=>'controller/actionIndex',

	),

),



May require some extra testing, but I know for a fact that when I try the third rule out with serverdomain/controller/abc/xyz/123/ and var_dump my $_GET’s, I have:

‘target’ => string ‘/abc/xyz/123’ (length=12)

To explain the rule a bit:

‘controller’ at start matches the word controller.

/* basically means “any amount of parameters afterwards”. I had to put this in since we can’t treat /abc/xyz/123 as ONE parameter.

Which is actually what we do anyway inside the gt/lt tags:

<target:\/\w+\/\w+\/\d+>

Here we say that what comes next is a GET parameter we want to name ‘target’. And the part to the right of the colon : is REGEXP. I didn’t know this from the start that this part was suppose to be regexp. Yet, it says so in the API reference, so checking there is fruitful at times! Try it!

So, once again:

<target:\/\w+\/\w+\/\d+>

The right part here is a simple regexp that matches basically:

/anyamountofletters/anyamountofletters/anyamountofdigits.

You’re welcome.

just lick tackle says:

have you read through this article?

Thats the post I used to learn the Yii Url manager.




'controller/<target:\/\w+\/\w+\/\d+>'=>'controllerName/index',

will actually rewrite a url looking like


http://mydomain.tld/controller/firstWord/secondWord/firstDecimal

to


http://mydomain.tld/index.php?r=controllerName/index&target=firstWord/secondWord/firstDecimal

is this what You were looking for?

btw note that controller in the first url is the literal string ‘controller’ not a dynamic variable

if you want to be able to assign the controller name as well you will need somthing like


'<controllerName>/*<target:\/\w+\/\w+\/\d+>'=>'<controllerName>/index',

NOTE: i have not tested these rules, just off the top of my head. :wink:

If you want meant that string "/xxx/yyy/123" are actyually three different variables say "/varOneName/varTwoName/varThreeName"

this becomes


'controllerName/<varOneName:\w+>/<varTwoName:\w+>/<varThreeName:\w+>'=>'controllerName/index'

NOTE: in the above please note that I use a \word regex to validate the parameter value.

so:




coolController/oneApple/twoOranges/ThreeEgs 

//will be rewritten to

index.php?r=coolController/index&varOneName=oneApple&varTwoName=tworangesOb&varThreeName=ThreeEgs

if you need to put other data into the parameter values use another regexp

cheers