bambinou
(Benoitrivaux)
March 12, 2015, 4:29pm
1
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…
bambinou
(Benoitrivaux)
March 12, 2015, 5:39pm
3
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()
jacmoe
(Jacob Moen)
March 12, 2015, 5:45pm
4
Dude. You are using echo with php echo tags!
Read Flarpy’s advice
jacmoe
(Jacob Moen)
March 12, 2015, 5:49pm
5
bambinou
(Benoitrivaux)
March 12, 2015, 5:59pm
6
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(); ?>
jacmoe
(Jacob Moen)
March 12, 2015, 6:47pm
7
The actual error message is not dangerous. Read it.
bambinou
(Benoitrivaux)
March 12, 2015, 8:16pm
8
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
jacmoe
(Jacob Moen)
March 12, 2015, 8:20pm
9
How are you passing $hello to the view?
jacmoe
(Jacob Moen)
March 12, 2015, 8:21pm
10
return $this->render('index', [
'hello' => $hello,
]);
bambinou
(Benoitrivaux)
March 12, 2015, 8:22pm
11
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…
jacmoe
(Jacob Moen)
March 12, 2015, 8:28pm
12
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,
]); ?>
bambinou
(Benoitrivaux)
March 12, 2015, 8:31pm
13
I see, thank you so much Jacmoe!