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)
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();
}
}
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).
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.