Showing Static Pages in the Yii Framework

April 17, 2013
The Yii Book If you like my writing on the Yii framework, you'll love "The Yii Book"!

Let’s look at a different way of rendering view files: using static pages. The difference between a static page and a standard page in the site is that the static page does not change based upon any input. In other words, a dynamic page might display information based upon a provided model instance, but a static page just displays some hard-coded HTML (in theory).

This is an excerpt from Chapter 7, “Working with Controllers,” of “The Yii Book“.

If you only have a single static page to display, the easy solution is to treat it like any other view file, with a corresponding controller action:

<!-- # protected/controllers/views/some/about.php -->
<h1>About Us</h1>
<p>spam, spam, spam...</p>

And:

# protected/controllers/SomeController.php
public function actionAbout() {
    $this->render('about');
}

The combination of those two files means that the URL http://www.example.com/index.php/some/about will load that “about” page. As I said, this is a simple approach, and familiar, but less maintainable when you have more static pages.

An alternative and more professional solution is to register a “page” action associated with the CViewAction class. This is done via the controller’s actions() method:

# protected/controllers/SiteController.php
public function actions() {
    return array(
        'page' => array('class' => 'CViewAction')
    );
}

{TIP} You can have any controller display static pages, but it makes sense to do so using the “site” controller.

The CViewAction class defines an action for displaying a view based upon a parameter. By default, the determining parameter is $_GET['view']. This means that the URL http://www.example.com/index.php?r=site/page&view=about or, if you’ve modified your URLs, http://www.example.com/index.php/site/page/view/about, is a request to render the static about.php page.

{NOTE} You must also adjust your access rules to allow whatever users (likely everyone) to access the “page” action. Or, you can do what the “site” controller does: not implement access control at all.

By default, CViewAction will pull the static file from a pages subdirectory of the controller’s view folder. Thus, to complete this process, create your static files within the protected/views/site/pages directory.

If, for whatever reason, you want to change the name of the subdirectory from which the static files will be pulled, assign a new value to the basePath attribute:

# protected/controllers/SiteController.php
public function actions() {
    return array(
        'page' => array(
            'class' => 'CViewAction',
            'basePath' => 'static'
        )
    );
}

You can also create a nested directory structure. For example, say you wanted to have a series of static files about the company, stored within the protected/views/site/pages/company directory. To refer to those files, just prepend the value of $_GET['view'] with “company.”: /site/page/view/company.board would display the protected/views/site/pages/company/board.php page.

By default, if no $_GET['view'] value is provided, CViewAction will attempt to display an index.php static file. To change that, assign a new value to the defaultView property:

# protected/controllers/SiteController.php
public function actions() {
    return array(
        'page' => array(
            'class' => 'CViewAction',
            'defaultView' => 'about'
        )
    );
}

To change the layout used to encase the view, assign the alternative layout name to the layout attribute in that same array.