URL rule for mapping custom links

My client has requested that they should be able to set the url text for pages they create on the site. They want to include keywords, whilst requiring the URLs be kept as short as possible. For example they require URLs like this:

localhost/computer-repairs-manchester

localhost/website-design-manchester

localhost/mobile-phones-north-west

So here we don’t include the controller name in the URL, however all the links belong to SiteController/actionPages(). The url text after ‘localhost’ is stored in a field ‘alias’ in the DB. This text does not contain any other parameters, so basically the controller action just needs to find the page from the ‘alias’ and display it.

What rule can I create to map these URLs to actionPages() ? Bear in mind there will potentially be hundreds of URLs like these, so the rule needs to be efficient.

BUMP. Anyone got any idea?

I thin that this cannot achive with CUrlManager.

Your only chance is to write your own urlManager, by extending CUrlManager and writing your own parseUrl() and createUrl() functions.

http://www.yiiframework.com/extension/dburlmanager

Hmmm interesting. What is performance like with this extension?

Hi GSTAR,

Having that url could just be:

‘/<view:\w+>’=> ‘site/page’

(not tested)

Hi Antonio,

Why is it ‘/<view:\w+>’ ? I don’t have a view parameter anywhere, the actual URL will be:


CHtml::link(CHtml::encode($model->label), array('site/pages', 'alias'=>$model->alias));

I think you might be confusing for the ‘page’ action, which renders static files.

Wow, very interesting extension. Thank you flavio for sharing.

OK now I am trying to include the ‘pages’ prefix in the URL but I cannot get it to work with this rule:

‘pages/<alias:\w+>’=>‘site/pages’,

Should let me access: localhost/pages/computer-repairs-manchester

It says "Unable to resolve the request "pages/computer-repairs-manchester"."

What is wrong with this rule? BTW I am able to navigate to localhost/site/pages/alias/computer-repairs-manchester


EDIT: It was because of the :\w+ part in the rule, I took it out and it works now.

Congratulations… i got confused with pages man…

So, has anybody tried out this extension - how well does it work?

How does it know that a URL belongs to a controller action (if typed in directly in the address bar)? What if I typed in mysite.com/invalid-page - would it give me a 404 page generated by sitecontroller or will it try to perform a match in the database and return an error from the controller action?

Hi [member=‘Flavio’], thanks for recommending the extension!

I’m glad you liked it, [member=‘zaccaria’] :)

[member=‘GSTAR’], there will be a proper 404 when the item is not found on the database.

You can discuss about dburlmanager at http://www.yiiframework.com/forum/index.php?/topic/11414-dburlmanager-extension/.

What about if a user insert a value that has the same name of an action?

  • If the URL rule for the extension comes before the rule for the action, the extension will check for the name on the database, and:

[indent][list]

  • If there’s a match on the database, the rule will be used;

  • If there isn’t, the rule will be ignored and the rule for the action will match later.

[/indent]

[*]If the URL rule for the extension comes after:

[indent]

  • The rule for the action will match before and will be used.

[/indent]

[/list]