如果我被迫从视图调用控制器方法,我该如何重构我的代码?


I'm forced to call a controller method from a view, how do I refactor my code?

好的,我正在使用CodeIgniter。posts.php是我的视图,显示所有的帖子,每个帖子必须显示其相应的评论,这是我想要实现的。

我在我的模型中有一个方法,它取postid($postid)并返回其相应的注释($comment),除非我通过控制器方法调用模型方法,我该如何实现这一点?

这是我的观点:

<body>
    <?php foreach ($post as $key):?>
    <div class="container">
        <div class="span10">
        <div id="box" class="alert-message block-message info">
            <div id="post" class="post">
                <?php echo $key->content;?><br />
            </div>
            <div>
            <p><?php //echo $comment;?></p> <!--HERE THE COMMENTS OF THE CORRESPONDNING POST MUST BE ECHOED-->
            </div>
            <div>
            <a href="#" id="commentnow<?php echo $key->postid;?>"><p><em>Comment</em></p></a>
            </div>
            <div id="commentarea<?php echo $key->postid;?>">
            <?php $name=array('name'=>"form$key->postid");
                  echo form_open("/welcome/comments/$key->postid",$name);
                  $data=array(
                                'id' => 'input',
                                'name'=> 'content',
                                'rows' => '2',
                                'placeholder' => "Write a comment...",
                                'autofocus' => 'TRUE'             
                      );
                  echo form_textarea($data);    
            ?>
            <a href="JAVASCRIPT:form<?=$key->postid;?>.submit()" id="cbtn" class="btn primary small">Comment</a>
            <?=form_close();?>
            </div>
        </div>
    </div>
    </div>
    </div>
    <script type="text/javascript">
    $(function(){
    $("div#commentarea<?=$key->postid;?>").hide(); 
    $('a#commentnow<?=$key->postid;?>').click(function(){
        $("div#commentarea<?=$key->postid;?>").slideToggle(250);
        }); 
    });
    </script>
    <?php endforeach;?>
    </body>

这是我的控制器方法,它返回与postid对应的注释:

public function comments($postid)
{
    //Post Comment
    $comment=$this->input->post('content');
    $data=array('content'=>$comment,'comment_postid'=>$postid);
    $this->comments->postcomment($data);
    //Retrieve
    $comments['comment']=$this->comments->retrieve($postid);
    $this->load->view('posts',$comments);
}

我是一个新手,如果我的代码不好,请原谅我。我一直期待着改进我的代码>谢谢你的耐心。:)

看看你的代码,在我看来,你还没有完全理解如何使用MVC

首先,你的控制器方法comments包含了对评论的提取。在查看视图文件时,这似乎不符合逻辑。

你应该把这两个分开。

在控制器中,添加另一个名为*post_comment*的方法,并将添加注释功能移动到该方法中,然后添加重定向:

public function post_comment($postid)
{
    //Post Comment
    $comment=$this->input->post('content');
    $data=array('content'=>$comment,'comment_postid'=>$postid);
    $this->comments->postcomment($data);
    redirect('welcome/comments'); //redirect back to comments
}
现在,从控制器中的comment方法中删除添加的注释,以便只检索注释:
public function comments($postid)
{
    //Retrieve
    $comments['comment']=$this->comments->retrieve($postid);
    $this->load->view('posts',$comments);
}

最后改变你的视图文件-你需要发布评论到一个新的URL:

        <?php $name=array('name'=>"form$key->postid");
              echo form_open("/welcome/post_comment/$key->postid",$name);