Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3375

[Wiki] How to display static pages in Yii with database content?

$
0
0

To extend further static pages as shown by Qiang http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in-yii/ here're the steps to take:

  1. Create a table spage (static page) in the databse with fields (id, url, title, content)
  2. Create model and CRUD for it via Gii
  3. Add method in the model Spage.php
public function getSpageByUrl($url) {
    return $this->find('url=:url',array(':url'=>$url));
}

4.. In protected/components/SiteController.php add

public function actionPage() {
    if(empty($_GET['view']))
        $this->actionIndex();
    $model = Spage::model()->getSpageByUrl($_GET['view']);
    if ($model === NULL)
        $this->actionIndex();
    else
        $this->render('pages/spage', array('model'=>$model));
}

5.. Create a view file: protected/views/site/spages/spage.php with content

<h1><?php echo $model->title?></h1>
<?php echo $model->content?>

6.. Create a page via the admin, e.g. with a url = "test" and sample data for title and content and navigate to it http://example.com/page/test - assuming that friendly URLs are turned on and index.php is hidden with config urlManager(array('urlFormat'=>'path','showScriptName'=>false,))

Hint: To remove "page" from the URL a line could be added at the start of the URL rules array:

'<view:\w+>' => 'site/page',

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.


Viewing all articles
Browse latest Browse all 3375

Trending Articles