1.9. Read One Article

We can see a list of blog articles, but now we want to be able to read a single article.

1.9.1. The "Read" Action Method

Open the Blog.php page controller class file. (In this example we use the vim editor, but you can use anything you like.)

$ vim Blog.php

Add the following PHP code to the class.

<?php
    public function actionRead($id = null)
    {
        // was an ID specified?
        if (! $id) {
            return $this->_error('ERR_NO_ID_SPECIFIED');
        }
    
        // fetch one blog article by ID
        $this->item = $this->_model->blogs->fetch($id);
    
        // did the blog article exist?
        if (! $this->item) {
            return $this->_error('ERR_NO_SUCH_ITEM');
        }
    }

[Note] URIs and Method Params

In Solar, the URIs are always in the format /controller/action/param1/param2/.... That is, the controller name is always first, the action name is second, and parameters for that action come last. The params in the path-info are passed to the action as method params. So, the a URI /blog/read/1 would map to a PHP call of Acme_App_Blog::actionRead(1).

If the URI is only /controller, then the controller will use the default action as defined in $_action_default.

1.9.2. The "Read" View Script

Create a view called read.php for the actionRead() method.

$ vim Blog/View/read.php

Enter the following code in the new read.php view script.

<?php $this->head()->setTitle($this->item->title); ?>

<h2><?php echo $this->escape($this->item->title); ?></h2>

<?php echo $this->nl2p($this->item->body); ?>

Essentially, this view takes a single record ($this->item) and presents it to the browser.

You should now be able to browse to http://localhost/index.php/blog/read/1 and see the full blog article.



Local