If you delete a comment, the article itself will be deleted.

Asked 1 years ago, Updated 1 years ago, 59 views

What I want to do is delete the comments.
I would like to redirect to the detail page of the article.

However, if you delete the comment,
The article itself will be deleted and redirected to the front page...
Thank you for your advice.

[CommentsController]

<?php

class CommentsController extensions AppController {
    public$helpers=array('Html','Form');



    public function add() {
        if($this->request->is('post'))){
            if($this->Comment->save($this->request->data)){
                $this->Session->setFlash('Success!');
                $this->redirect(array('controller'=>'posts', 'action'=>'view', $this->data['Comment']['post_id']));
            } else{
                $this->Session->setFlash('failed!');
            }
        }
    }

    public function delete($id){
        if($this->request->is('get')){
            through new MethodNotAllowedException();
        }
        if($this->Comment->delete($id)){
            $this->Session->setFlash('Deleted!');
            $this->redirect(array('controller'=>'posts', 'action'=>'view', $this->data['Comment']['post_id']));
        }
        // $this->redirect(array('controller'=>'posts', 'action'=>'index'));
    }
}


?>

[view.ctp]

<h2><?phpechoh($post['Post']['title']);?>/h2>

<p><?phpechoh($post['Post']['body']);?>/p>

<h2>Comments<h2/>
<ul>
<?php foreach ($post['Comment'] as $comment): ?>
<li>
<?phpechoh($comment['body'])?>by<?phpechoh($comment['comment']);?>
<?php
    echo $this->Form->postLink('delete', array('action'=>'delete', $post['Post']['id']), array('confirm'=>'sure?'));
?>
</li>
<?php endforeach;?>
</ul>


<h2>Add Comment</h2>

<?php
echo$this->Form->create('Comment', array('action'=>'add'));
echo$this->Form->input('commenter');
echo$this->Form->input('body',array('rows'=>3));
echo$this->Form->input('Comment.post_id',array('type'=>'hidden','value'=>$post['Post']['id']));
echo$this->Form->end('post comment');



?>

php cakephp

2022-09-30 20:34

1 Answers

view.ctp

 echo$this->Form->postLink('delete', array('action'=>'delete', $post['Post']['id']), array('confirm'=>'sure?'));

This code is completely for Post deletion.First, the controller name is not specified, so it is interpreted as the PostsController currently running.The ID I gave you is also Post's.

Therefore, it needs to be corrected as follows.

 echo$this->Form->postLink('delete', array('controller'=>'comments', 'action'=>'delete', $comment['id']), array('confirm'=>'sure?'));


2022-09-30 20:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.