Urlmanager Question

I’m trying to get the UrlManager to handle me a request but I can’t get it to work. Can anyone with more experience perhaps make a suggestion?

What I’m trying to accomplish…

My url looks like this:

www.example.com/apiinterface/1/key/myaction&id=1001

I’ve updated my .htaccess to rewrite the url as follows:




RewriteRule ^apiinterface/([0-9])/(.*)/(.*)$ index.php?r=apiinterface/$3&APIVersion=$1&APIKey=$2 [NC,L]



I’ve tested this rule and it works. My goal is to adress the ‘ApiinterfaceController’ with the ‘myaction’ and get all the other values as parameters.

I’ve also added an rule in the UrlManager to make Yii understand the url.




'rules'=>array(

  'apiinterface/<apiversion:\d+>/<apikey:\w+>/<action:\w+>'=>'apiinterface/<action>',

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

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

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

),



When opening (www.example.com/apiinterface/1/key/myaction&id=1001) I get a 404 error:


The system is unable to find the requested action "1".

I get the feeling that Yii isn’t processesing the url as it was rewritten by apache and/or url rule isn’t working as I want it to.

Should I even use the .htaccess rule and/or how do I get the CUrlManager to understand the request as I want it?

Cheers.

A quick reply…

You’re using something called pretty paths (or SEO-optimized URLs) so I thing sure, you’ll have to use .htaccess. There is no way, this gonna work without it.

Second question, is why do you have to complicate your life (and your URLs :]) that hard? Can’t you use a standard solution, provided by Yii?

It consist of setting urlManager:

[size="2"]


'showScriptName'=>false,

'urlFormat'=>'path',

'urlSuffix'=>'.html',

'rules'=>array

(

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

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

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



[/size][size="2"]Then, writting correct .htaccess code:[/size][size="2"]


Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


# If file or folder exists -- use it directly...

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


# ...otherwise redirect to index.php

RewriteRule . index.php


DirectoryIndex index.php

Options -Indexes

[size=“2”]And then you’re ready to go, using URLs like this:[/size]


http://localhost/main/show.html?page=markdown

[size="2"]where [/size]main[size="2"] is your controller part of route and corresponds to [/size]protected/controlers/MainController.php[size="2"] and [/size]show[size="2"] is your action (function [/size]actionShow()[size="2"] inside [/size]MainController.php[size="2"]) and rest are your action parameters (here only one – so: [/size]actionShow($page)[size=2]).[/size]

That seems to be the easiest solution and is confirmed to be working as a charm.

Thanks for the reply.

Unfortunately the url format was forced onto me because it is actually an Android application that was developed by an other company. First thing I asked was if they could change their url format to suit me a bit better; there’re still thinking on that…

Anyway, for me I was a nice challenge to try and understand if I chose the right framework and if I could even fix a silly thing like this easily using the UrlManager. I’ve not been using Yii that long and I’m still learning here. I find the documentation a bit weak, missing some examples as a starting point and although the UrlRules were documented better I couldn’t figure out if my idea was even remotely possible.

But you’re probably right, conforming to Yiis standard solution is definitly easier (but not always as interesting ;)

What can I sell else?

CUrlManager is based on regular expressions, which were always a terrible mystery for me. I know and understand how powerful and flexible they’re, but I never managed to learn them at least at basic level.

So, each time I hear or think about CUrlManager modification, I simply thrill! :]

If you want to learn about regex than I realy recommend the PDF below.

I have one lying around my desk at all time, printed and laminated. I didn’t understand regex as well, but this cheat sheet helped me understand them and now I can’t do without it and I’m realy loving regexes these days :)

@Arno S: That’s certainly the best regular expressions cheat-sheet I’ve ever seen, thanks! :]

Thanks for pointing out that CUrlManager uses regular expressions. In hindsight that’s obvious but I somehow didn’t see it immediately.

Turns out that I didn’t need to change my .htaccess file. The rule leading towards the Yii index.php was enough. All I wanted can be done using the Yii UrlManager but writing a fitting regex can be a challenge…

Well… I thought, you know, that CUrlManager uses regexp and you only have problems modelling proper rules! :]

So, yes… you can leave .htaccess untoched and try to solve your problem by modifing CUrlManager rules only.

Or my my extend CUrlManager to your own class, overload createUrl and parseUrl functions there, and try to solve your problem that way. It was explained for example in this Wiki article.