step 12: making add post view controller cakephp blog
Open your browser to see the effect before modifying controller "http://localhost/cakeblog/posts/add" in cakephp blog. Opps!!! Your Browser output should look like as follows:
To add post working smoothly we need to make two changes as follows:
Change #1 - Controller Modification:
Open posts Controller located at “/app/controllers/posts_controller.php" then copy & past the blow code replacing current.
/app/controllers/posts_controller.php
<?php
class PostsController extends AppController
{
var $name = 'Posts';
function index()
{
$this->set('posts', $this->Post->find('all'));
}
function view($id = null)
{
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
function admin()
{
$this->set('posts', $this->Post->find('all'));
}
function add()
{
if (!empty($this->data))
{
if ($this->Post->save($this->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'admin'));
}
}
}
}
?>
Agin open your browser to see the effect before making view to add post "http://localhost/cakeblog/posts/add" in cakephp blog. Opps!!! Your Browser output should look like as follows:
Change #2 - view post details addition:
Make a file at “/app/views/posts/add.ctp" then copy & paste blow code into it to make the details view of the post.
/app/views/posts/add.ctp
<!-- File: /app/views/posts/add.ctp -->
<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>
Now open your browser and see the add form to add post "http://localhost/cakeblog/posts/add".
Finally saving data will guide you to as follows:
Related Tutorial Examples
- step 9: view for posts cakephp blog cakephp examples
- step 10: making view post details for cakephp blog
- step 11: admin view posts for cakephp blog
- step 13: edit post view controller cakephp blog
- step 14: delete post view controller cakephp blog
- step 15: adding element in cakephp blog
- step 16: passing data into element in cakephp blog
- MySQL IF() Function Boolean Value Example
- MySQL strcmp() Function Number Value Example
- MySQL strcmp() Function NULL Value Example




No comments:
Post a Comment
leave your comments here..