Yii 1.1: How to display static pages in Yii?

14 followers

In a Web application, we often need to display pages like "about this site", "legal information", whose content are mostly static. There are several approaches to deal with this kind of pages.

  • We can store these pages in individual HTML files so that the Web server will directly serve them to end users without incurring PHP. The drawback is that it is a hassle to maintain the common layout shared by these static pages and the other dynamic pages.

  • We can write an action and render the corresponding view file for each of the static pages. This resolves the layout problem, but it is too much trouble to write an action for each page, especially when the only work the action does is to render a view.

  • We can use a single CViewAction to serve all these pages. Below we will show you how this approach works.

First, in the default SiteController (or other controller if you like), override the actions() method as follows,

public function actions()
{
    return array(
        'page'=>array(
            'class'=>'CViewAction',
        ),
    );
}

According to the Guide, the above code declares an external action named page whose class is CViewAction.

Second, create a folder protected/views/site/pages.

Third, save each static page as a PHP file under this folder. For example, we can save the "about this site" page as about.php. Note, these pages will use the application's default layout. Therefore, only the main content needs to be saved in each file.

We are done! To access a static page, e.g., the about page, we can use the following URL:

http://www.example.com/index.php?r=site/page&view=about

To beautify this URL, we may use the approach described in the Guide.

If we have many static pages, we may organize them into sub-folders under protected/views/site/pages. We can use the following URL to access a page that is stored as protected/views/site/pages/help/contact.php:

http://www.example.com/index.php?r=site/page&view=help.contact

We may customize CViewAction if you do not like the above setup. For more details, please check the API documentation of CViewAction.

Links ΒΆ

Total 2 comments

#9850 report it
yasen at 2012/09/15 04:03am
Already in the core

Not sure from which version of Yii, but in 1.1.12

'page'=>array(
  'class'=>'CViewAction',
),

is included in SiteController::actions()

Edit: Dared to create a Wiki article explaining how to show static pages with database content http://www.yiiframework.com/wiki/403/how-to-display-static-pages-in-yii-with-database-content/

#8760 report it
mem at 2012/06/25 06:58am
Don't forget accessRules

If you happen to use accessRules, don't forget to add 'page' (or whatever name you have in) to allowed action as well.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: qiang
  • Updated by: resurtm
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +19
  • Viewed: 47,656 times
  • Created on: Mar 17, 2009
  • Last updated: Feb 22, 2013
  • Tags: static pages