We can see a list of blog articles, but now we want to be able to read a single article.
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');
}
}
URIs and Method Params | |
---|---|
In Solar, the URIs are always in the format
If the URI is only |
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.