How to pass a variable from view file to controller?

Hi everybody,

I just have done the Yii Blog tutorial and now i want to show all the comments in the post/index.php (where you are able to see all posts in a list) which worked fine so far, but the thing that doesn’t work is commenting. Because the form doesn’t know the post id.

So my question is, how can I pass the post id to the postController? Or is there an even better solution?

Thanks a lot,


If you are displaying the post page, you should have a $model variable with the model.

Here you have all data of the post. Use a form with an action like:

'action'=>array('post/comment', 'id'=>$model->id)

And you will got in the action comment a $_GET variable with the id of the post.

Thanks for your help so far. But I couldn’t solve the problem yet.

The thing is, that i got a list of "posts" (I called them "entrys") shown on the page and each can be commented.

So my _view (entry) looks as follows:

<div class="view">

    <?php echo CHtml::link(CHtml::encode($data->translations[0]->title), array('view', 'id' => $data->entry_id)); ?>, 

    <?php echo Yii::app()->locale->dateFormatter->formatDateTime($data->update_time, 'long', 'medium') ?>, von

    <?php echo $data->user->username; ?>,

    <?php if ($data->commentCount >= 1): ?>

    <?php echo $data->commentCount . ' Kommentare'; ?>

    <?php endif; ?>

    <?php echo $data->translations[0]->content; ?>

        <br />

        <div id="comments">

        <?php if (Yii::app()->user->hasFlash('commentSubmitted')): ?>

            <div class="flash-success">

            <?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>


        <?php else: ?>


                $this->renderPartial('/comment/_form', array(

                    'model' => $data->comments,

                    'entry' => $data,



        <?php endif; ?>

        <?php if ($data->commentCount >= 1): ?>


                    $this->renderPartial('_comments', array(

                        'entry' => $data,

                        'comments' => $data->comments,

                        'translation' => $data->comments->translations[0],

                    )); ?>

        <?php endif; ?>

    </div><!-- comments -->


The called _form (entry) looks like that:

<div class="form">


    $form = $this->beginWidget('CActiveForm', array(

                'id' => 'comment-form',

                'enableAjaxValidation' => true,



    <?php echo $form->errorSummary($model); ?>

    <div class="row">

        <?php echo '<p style="font-size:18px; color:#000;">Wie sehen Sie das? </p>' ?>

        <?php echo $form->textArea(Translation::model(), 'content', array('rows' => 1, 'cols' => 50)); ?>

        <?php echo $form->error(Translation::model(), 'content'); ?>


    <div class="row buttons">

        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>


    <?php $this->endWidget(); ?>

</div><!-- form -->

And the _comments (entry) looks like that:

<?php foreach ($comments as $comment): ?>

    <div class="comment" id="c<?php echo $comment->comment_id; ?>">

    <div class="content">


        <?php echo nl2br(CHtml::encode($comment->translations[0]->content)); ?>



    <div class="author">

        von <?php

        if ($comment->user->username == null) {

            echo $comment->authorLink;

        } else {

            echo $comment->user->username;



        <?php echo Yii::app()->locale->dateFormatter->formatDateTime($comment->create_time, 'long', 'medium') ?>


    <div class="time">



</div><!-- comment -->

<?php endforeach; ?>

In the EntryController I thought of making the following call in the actionIndex Method:

$comment = $this->newComment($this->loadModel());

The newComment function needs an entry model object…

And now the newComment function in EntryController should receive the correct entry_id (belonging to the posted comment):

public function loadModel()


        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                if (Yii::app()->user->isGuest)

                    $condition = 'status=' . Entry::STATUS_PUBLISHED

                            . ' OR status=' . Entry::STATUS_ARCHIVED;



                $this->_model = Entry::model()->with('translations')->findByPk($_GET['id'], $condition);


            if ($this->_model === null)

                throw new CHttpException(404, 'The requested page does not exist.');


        return $this->_model;


Just cant figure out how to solve this … there must be an easy solution but i’ve now tried so much, that Im even more confused :-D. still learning Yii :wink:

Thanks in advance,