如何从视图Yii-2中的compose()获取参数?
我试试:
/控制器/ SiteController
Yii::$app->mailer->compose('layouts/html.php', ['model' => 'trtrtrtrt',])
->setFrom('ergegergerger@gmail.com')
->setTo('ergergergegerg@mail.ru')
->setSubject('TEST')
->send();
邮件/布局/ 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() ?>
我收到错误“未定义变量:模型” - <?= Html::encode($model) ?>
如何从compose()中获取参数?
答案 0 :(得分:2)
您正在调用布局文件中的参数。但参数model
仅在视图中可用。
所以你必须改变代码:
邮件/布局/ 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() ?>
邮件/ emailview.php 强>
<?= Html::encode($model) ?>
php代码
Yii::$app->mailer->compose('emailview.php', ['model' => 'trtrtrtrt',])
->setFrom('ergegergerger@gmail.com')
->setTo('ergergergegerg@mail.ru')
->setSubject('TEST')
->send();