Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 pass parameters to mail layout

Is there any way to access your parameters within a mail layout when using the Yii mailer class? I can access $model from the view but not the layout.

<?php

        $params = array(
           "model" => $model
        );

        $message = Yii::$app->mailer->compose([
            'html' => $view.'Html',
            'text' => $view,
        ], $params)
        ->setFrom("[email protected]")
        ->setTo($recipient)
        ->setSubject($subject);
?>

I know that for a standard web view you would set yii\web\View::$params to access variables in the layout but this doesn't appear to work for the mailer.

Any ideas?

like image 402
Neil Bryson Avatar asked Aug 13 '15 14:08

Neil Bryson


2 Answers

I just found out another way to set params to layout from controller :

// In your controler before send mail : 
Yii::$app->mailer->view->params['title'] = $title;

// In your layout
echo $this->params['title'];

Hope this help!

like image 194
Luong Tran Nguyen Avatar answered Nov 19 '22 00:11

Luong Tran Nguyen


I'd do something similar to how you get breadcrumbs back to the view.

//In your view file, model was passed down via compose as you have it.
//this adds model element to the View object's params.
$this->params['model'] = $model; 

//In your layout
echo $this->params['model']->attribute;
like image 21
Kirk Avatar answered Nov 19 '22 01:11

Kirk