how get parameters from compose() in view Yii-2?

不打扰是莪最后的温柔 提交于 2019-12-23 02:43:07

问题


how get parameters from compose() in view Yii-2?

I try:

/controllers/SiteController

Yii::$app->mailer->compose('layouts/html.php', ['model' => 'trtrtrtrt',])
 ->setFrom('ergegergerger@gmail.com')
 ->setTo('ergergergegerg@mail.ru')
 ->setSubject('TEST')
 ->send();

mail/layouts/html.php

<?php
use yii\helpers\Html;
use yii\mail\BaseMailer;


/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <title><?= Html::encode($this->title) ?>Тестовое письмо</title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>

    <div style="background-color: green;">Приветик !</div>

     <?= Html::encode($model) ?>

    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

i get error "Undefined variable: model" - <?= Html::encode($model) ?>

how get parameters from compose()?


回答1:


You are calling a parameter in the layout file. But the parameter model is available only in the view.

So you have to change the code:

mail/layouts/html.php

<?php
use yii\helpers\Html;
use yii\mail\BaseMailer;


/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <title><?= Html::encode($this->title) ?>Тестовое письмо</title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>

    <div style="background-color: green;">Приветик !</div>

     <?= $content ?>

    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

mail/emailview.php

<?= Html::encode($model) ?>

php code

Yii::$app->mailer->compose('emailview.php', ['model' => 'trtrtrtrt',])
 ->setFrom('ergegergerger@gmail.com')
 ->setTo('ergergergegerg@mail.ru')
 ->setSubject('TEST')
 ->send();


来源:https://stackoverflow.com/questions/35969709/how-get-parameters-from-compose-in-view-yii-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!