Chapter 5. Viewing a post

We are now going to create a page named view to view a single post.

The page will need a GET parameter named id which must contain the id of a post. Let's create the action file (app/actions/view.php) with this simple line:

			
<?php

if (!Atomik::has('request/id')) {
	Atomik::flash('Missing id parameter', 'error');
	Atomik::redirect('index');
}

$post = Atomik_Db::find('posts', array('id' => A('request/id')));
		

First we check if the id parameter is set. If not we create a flash message and redirect the user to the index page. Otherwise, we fetch the requested post from the database.

The view (app/views/view.phtml) is also very simple:

			
<h1><?= $post['title'] ?></h1>
<p>
	Published the <?= $post['publish_date'] ?>
</p>
<p>
	<?= $post['content'] ?>
</p>
		

Finally, we're going to modify the index view to add a link on post titles. Replace the line where the post title is echoed with:

			
<li>
	<a href="<?= Atomik::url('view', array('id' => $post['id'])) ?>"><?= $post['title'] ?></a>
</li>