Can I route urls from the database with Yii?

Hi,

Yii looks really impressive, but before I dive in, I’d just like to know is it possible to route urls from the database? (for some projects I require absolute custom urls that are only one segment - e.g - domain/title-of-something , yet would use different controllers, some of these projects are quite large so using the database is the only realistic option)

Thanks for any help.

(sorry if I’ve posted this in the wrong forum…)

Hi, the core of Yii doesn’t support this feature, but it’s easy to create a special component that will do what you need.

If I got it right you want to fetch all url-rules upon application start from the database. An easiy solution would be to override processRules() of CUrlManager.




class MyUrlManager extends CUrlManager

{


   public $dbTable = 'UrlRules';


   protected function processRules()

   {


      $dbCommand = Yii::app()->db->createCommand("

         SELECT `pattern`, `route` FROM `{$this->dbTable}`

      ")->execute();


      $urlRules = $dbCommand->readAll();


      foreach ($urlRules as $pattern => $route)

      {

         $this->rules[$pattern] = $route;

      }


      parent::processRules();


   }


}



Then in config:




'components' => array(

   ...

   'urlManager' => array(

      'class' => 'MyUrlManager',

   ),

   ...

),



Should give you an idea on how to implement such functionality.

That looks the sort of thing I am after, I didn’t really expect it to be part of the core, just whether it was possible without having to hack anything (I’ve been looking over several frameworks and some of them (Codeigniter for instance) can’t really do this without re-writing a couple of core classes).

Thanks for your help, I’ll give Yii a try.

Okay nice to hear. I don’t know CodeIgniter but Yii is very extendable - should fit your needs. ;)

I’m trying to output my menu items from a database and I’m having a problem. Basically, I’m storing the controller/view path in a field and using CHtml::link to output the data. It seems to work fine except when I have a link to a module that only uses one item, like “post” (no controller/view format).

For example when this is stored in the database

site/index

A correct link is displayed

But when this is stored in the database:

post

I get site/post which is wrong. I need it to just be "post"

I’m using the blog example if this helps any, and I created a component in a module and have the component on the main layout for my site. Again all the menu items that have a controller/view work fine, but without both values yii automatically puts a controller value in the output. I could not use the CHtml::link class and format the url myself but I was wondering if anybody else had this problem and how they might have fixed it.

Does this help?

createUrl method is used by CHTML’s link(), here’s what the docs say:

http://www.yiiframework.com/doc/api/CController#createUrl-detail

Intel352,

Thanks for the hint about the forward slash. Yes, this does solve the problem.

hi,

I have created a table for storing custom urls, the values in table are as follows

for college1:

pattern1=>a-p-s-college-of-arts-and-science

route1=>institute/view/id/6137

for college2:

pattern2=>a-p-s-evening-college-of-arts-commerce

route2=>institute/view/id/6110

when i browse

siteUrl/a-p-s-college-of-arts-and-science

siteUrl/a-p-s-evening-college-of-arts-commerce

both the link out puts the correct data for the views, it seems the one way is working fine.

Still I can see the display urls are in form of

siteUrl/institute/view/id/6137

siteUrl/institute/view/id/6110

it is not automatically converted to

siteUrl/a-p-s-college-of-arts-and-science

siteUrl/a-p-s-evening-college-of-arts-commerce

How can we achieve both way conversion?

Please help…