Can't output variable from controller to view...why?

Hello,

I wonder why I cannot output my variable called $hello in my _form.php view

Here is the code:




    public function actionCreate()

    {

        $hello = 'Hello Mate';

        $model = new Posts();




        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->posts_id]);

        } else {

            return $this->render('create', [

                'model' => $model,

                'hello' => $hello,

            ]);

        }

    }



Here the _form.php view file




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


/* @var $this yii\web\View */

/* @var $model frontend\models\Posts */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="posts-form">


    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'posts_title')->textInput(['maxlength' => 100]) ?>


    <?= $form->field($model, 'posts_description')->textarea(['rows' => 6]) ?>


    <?= $form->field($model, 'author_id')->textInput() ?>


    <?= echo $hello; ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>



Thank you for your help,

I am sure it is stupid but cannot work it out…

Your problem is here




 <?= echo $hello; ?>



Try




 <?= $hello; ?>



or




 <?php echo $hello; ?>



Thank you for helping,

For all 3 solutions I am getting this error:




PHP Parse Error – yii\base\ErrorException


syntax error, unexpected 'echo' (T_ECHO)

1. in C:\xampp\htdocs\yiiproject\frontend\views\posts\create.php at line 21

1213141516171819202122?>

<div class="posts-create">

 

    <h1><?= Html::encode($this->title) ?></h1>

 

    <?= $this->render('_form', [

        'model' => $model,

    ]) ?>

    <?= $this->render('_form', array('model' => $model, 'hello'=>$hello)); ?>

    <?= echo $hello; ?>

</div>

2. yii\base\ErrorHandler::handleFatalError()




Dude. You are using echo with php echo tags!

Read Flarpy’s advice

http://php.net/manual/en/ini.core.php#ini.short-open-tag

My apology, I pasted the wrong one,

Here is what is happening with normal PHP tags:




PHP Notice – yii\base\ErrorException


Undefined variable: hello

1. in C:\xampp\htdocs\yiiproject\frontend\views\posts\_form.php at line 21

121314151617181920212223242526272829 

    <?php $form = ActiveForm::begin(); ?>

 

    <?= $form->field($model, 'posts_title')->textInput(['maxlength' => 100]) ?>

 

    <?= $form->field($model, 'posts_description')->textarea(['rows' => 6]) ?>

 

    <?= $form->field($model, 'author_id')->textInput() ?>

 

    <?php echo $hello; ?>

 

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>

 

    <?php ActiveForm::end(); ?>



The actual error message is not dangerous. Read it. :)

But how can I have an "Undefined variable: hello" when I defined it in the controller? This is what I do not understand.

for me it looks like the controller cannot pass variables in that way in the views, if not, what method shall I use to pass a simple variable from a controller to a view please? Thanks

How are you passing $hello to the view?




        	return $this->render('index', [

            	'hello' => $hello,

        	]);



Yes




    public function actionCreate()

    {

        $hello = 'Hello Mate';

        $model = new Posts();




        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->posts_id]);

        } else {

            return $this->render('create', [

                'model' => $model,

                'hello' => $hello,

            ]);

        }

    }



I had a friend who tried to help me but could not either…we do not know what we are doing wrong. Perhaps it is simple but as we are still learning how to use Yii , we are not yet confident enough to work this problem out on our own sorry…

Ah, I see now.

You need to pass it on to the _form as well, in the view. :)

<edit>

So, edit views/posts/create.php and add ‘hello’ to the array of things to pass on to _form


	<?php echo $this->render('_form', [

	'model' => $model,

	'hello' => $hello,

	]); ?>



I see, thank you so much Jacmoe!