Skip to content

Commit

Permalink
Add the 'edit' method in PostController.
Browse files Browse the repository at this point in the history
Create the edit view and store the codes in app/view/posts/edit.ctp file
  • Loading branch information
stinkinkevin committed Oct 25, 2015
1 parent 5bf8675 commit 323e8b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/Controller/PostController.php
Expand Up @@ -53,5 +53,39 @@ function add() {
}
}
}

// This function handles updating post data
function edit($id=null) {
$actionHeading = 'Edit a Post!';
$actionSlogan = 'Please fill in all fields. Now you can amend your post.';

$this->set(compact('actionHeading','actionSlogan'));

// Check whether $id and $this->data are empty,
// or an error message will be stored in our 'Session' object,
// and the request is redirected to the blog home page.
if (!$id && empty($this->data)) {
$this->Session->setFlash(_('Invalid Post',true));
$this->redirect(array('action'=>'index'));
}

// If the submitted formdata is not empty,
// Cake will try to commit the edited post information to the posts database table
// and then flash appropriate messages upon success or failure.
// Finally, if only the submitted data is empty, a post's information is pulled
// with the 'Post' model 'read' method using the supplied $id as the criterion.
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash(_('The Post has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(_('The Post could not be saved. Please try again.', true));
}
}

if (empty($this->data)){
$this->data = $this->Post->read(null,$id);
}
}
}
?>
3 changes: 3 additions & 0 deletions app/View/Posts/edit.ctp
@@ -0,0 +1,3 @@
<?php
echo $this->element('add_or_edit');
?>

0 comments on commit 323e8b5

Please sign in to comment.