Difference between #12 and #16 of
How to display static pages in Yii with database content?

Changes

Title unchanged

How to display static pages in Yii with database content?

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

static pages, database

Content changed

[...]
2. Create model and CRUD for it via Gii
3. Add method in the model Spage.php


```php
public function
getSpagefindByUrl($url) {
return $this->find('url=:url',array(':url'=>$url));
}
[...]
if(empty($_GET['view']))
$this->actionIndex();
$model = Spage::model()->
getSpagefindByUrl($_GET['view']); if ($model === NULL)
 
$this->actionIndex(
// if page is not found, then run a controller with that name
 
if ($model === NULL)
 
Yii::app()->runController($_GET['view']
);
else
$this->render('pages/spage', array('model'=>$model));
[...]
so the address could simply be http://example.com/test

That's about it. Hope it's clear enough. Will update the article further to make it better. Happy coding'<view:[\w\-]+>'=>'site/page', now have priority over '<controller:[\w\-]+>'=>'<controller>/index' and the other rules with controller so if the static page is not found in the database, we will run a controller with that name Yii::app()->runController($controller);
 
 
7.. Now if you use the CMenu widget, you will notice that the static pages do not get highlighted on select. Here's a fix for that too. Create Menu.php file in /protected/components/ with:
 
 
```php 
class Menu extends CMenu{
 
protected function isItemActive($item,$route)
 
{
 
$pos=strpos(ltrim(Yii::app()->getRequest()->pathInfo,'/'),'/');
 
$route = $pos===false ? $pathInfo : substr($pathInfo,0,$pos);
 
return parent::isItemActive($item, $route);
 
}
 
}
 
```
 
Replace widgets.CMenu with widgets.Menu in your layout file/s.
 
 
That's about it. Hope it's clear enough. Will update the article further to make it better. Happy coding.
 
 
Update: Added controller execution on missing static page. Extended CMenu widget to highlight active static pages in the menu
.
3 0
13 followers
Viewed: 42 300 times
Version: 1.1
Category: Tutorials
Written by: yasen
Last updated by: yasen
Created on: Oct 11, 2012
Last updated: 11 years ago
Update Article

Revisions

View all history