1.8. Browse All Public Articles

Now that we have a basic application class prepared, we can start adding action methods and view scripts. We'll start with an index of published blog articles.

1.8.1. The "Index" Action Method

Change to the SYSTEM/source/acme/Acme/App/ directory and open the Blog.php page controller class file. (In this example we use the vim editor, but you can use anything you like.)

$ cd SYSTEM/source/acme/Acme/App/
$ vim Blog.php

Add the following PHP code at the top of the class inside the actionIndex() method.

    public function actionIndex()
        // public blog articles in descending order, all result pages
        $fetch = array(
            'where' => array('blogs.status = ?' => 'public'),
            'order' => 'blogs.created DESC',
            'page'  => 'all',
        // fetch all matching records
        $this->list = $this->_model->blogs->fetchAll($fetch);

A bit of explanation:

  • The $fetch array is a series of clause definitions to say what records we want to retrieve. There are keys for each part of an SQL SELECT command: cols, where, join, group, having, order, and limit. In addition, there are other keys related to model fetches, such as page and paging to indicate which page-number of results to retrieve and how many results per page.

  • The $this->list property is populated by a call to the blogs model. In this case, we fetch all records (a "collection") that match our $fetch parameters.

[Note] Note

Public properties of the controller are automatically assigned to the view at render-time. You don't need to assign them yourself.

1.8.2. The "Index" View Script

By default, Solar page controllers expect to render a view script named for the action being invoked. As such, we need to create a view called index.php for the actionIndex() method.

$ vim Blog/View/index.php

[Note] View Inheritance

Page controller view scripts are inherited from their parent controllers. Thus, an index.php view script from a parent controller will be used if no such view script exists for the current page controller.

Enter the following code to replace whatever else might be there.

    $title = $this->getTextRaw('TITLE_INDEX');

<h2><?php echo $this->getText('HEADING_INDEX'); ?></h2>

<?php if (! $this->list): ?>

    <p><?php echo $this->getText('ERR_NO_RECORDS'); ?></p>

<?php else: ?>

    <?php foreach ($this->list as $item): ?>
            echo $this->escape($item->title);
            echo "&nbsp;&nbsp;";
            echo $this->action(
    <?php endforeach; ?>
<?php endif; ?>

Essentially, this view takes the collection of records in $this->list and presents them as an unordered list, with a link to read each one.

  • Public properties from the page controller become properties of the view object. For example, $this->list refers to the $list property from the page controller.

  • The page controller creates a few view properties automatically for you. They are $this->controller_class, $this->controller, $this->action, $this->layout, and $this->errors.

  • All variables not prefixed with $this are local to the view script itself. That way, it's easy to tell what is from the controller, and what is created by the view.

  • The methods $this->getTextRaw(), $this->head(), $this-getText(), $this->escape(), and $this->action() are called "helpers". They encapsulate common bits of code that are used over and over within views. You can see all the available helpers in the Solar_View_Helper and Solar_View_Helper_Form packages.

  • The use of all-capital text strings instead of literal text is to prepare for localization later.

You should now be able to browse to http://localhost/index.php/blog and see a listing of exactly one public blog article.