php
Please login
=$message;?>This will display the following screen:
app/views/auth/register.php
Please register your account
This will display the following screen:
Now comes the controller which will handle the blog operations
The code in the app/controllers/blog.php is as follows:
session_start();
class blog extends controller
{
public function display()
{
$user = $_SESSION['userid'];
$posts = $this->model->post->find(array("user_id"=>$user),10);
if(!$posts)
{
$this->redirect("blog","write");
}
else
{
foreach ($posts as &$post)
{
$post['comments']=$this->model->comment->find
(array("post_id"=>$post['id']));
}
$this->view->set("posts",$posts);
Building Better with MVC
[ 246 ]
}
}
public function post()
{
$postid= $this->params['0'];
if (count($_POST)>1)
{
$comment = $this->model->comment;
$comment->date = time();
$comment->post_id = $postid;
$comment->insert();
}
$post = $this->model->post->find(array("id"=>$postid));
if (!empty($postid))
{
$post[0]['comments'] = $this->model->comment->find
(array("post_id"=>$postid),100);
}
$this->view->set("message","");
$this->view->set("post",$post[0]);
//die($postid);
}
public function write()
{
$this->view->set("color","green");
if (!empty($_POST))
{
$post = $this->model->post;
$post->user_id=$_SESSION['userid'];
$post->date = time();
$post->insert();
$this->view->set("color","green");
$this->view->set("message","Successfully saved
your blog post");
}
}
}
?>
Chapter 9
[ 247 ]
And here are the views of our blog controller:
app/views/blog/display.
Pages:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242