Hello, I'm trying to figure out what could be happening when using getRender() to render an email template (as Vökuró tutorial does). The method works as expected, but I have a dispatcher->forward after the email is sended, and this dispatcher no longer works (blank page) if I use the getRender() method to get the content for the email.
The email is sended allright with the template I provide to the getRender() method, but someting is happening between because instead of forwarding to other action I get an blank page, only with a direct flash messages I created before.
As soon as I substitute getRender() in the code with a string, the dispatcher starts to work.
If instead of using the dispatcher->forward I use a response->redirect, It works. But, of course, I can't use Phalcon\Flash\Direct.
I used to create this project a similar way as Vökuró tutorial does (In Vökuró works fine), but using modules, this is:
A SignUp method inside a SessionController:
app\modules\auth\controllers\SessionController.php
namespace MyApp\Modules\Auth\Controllers;
class SessionController extends ControllerBase
{
public function signupAction()
{
...
if ($user->save()) {
return $this->dispatcher->forward([
'namespace' => 'MyApp\Modules\Auth\Controllers',
'controller' => 'index',
'action' => 'index'
]);
}
....
}
}
An afterSave() method inside User class to create an EmailConfirmation:
app\models\User.php
namespace Vcloud\Models;
...
class User extends \Phalcon\Mvc\Model
{
...
public function afterSave()
{
$emailConfirmation = new EmailConfirmation();
$emailConfirmation->user_id = $this->id;
if ($emailConfirmation->save()) {
$this->getDI()
->getFlash()
->notice('A confirmation mail has been sent to ' . $this->email);
}
}
...
}
An afterCreate() method inside EmailConfirmation to send the email:
app\models\EmailConfirmation.php
class EmailConfirmation extends \Phalcon\Mvc\Model
{
...
public function afterCreate()
{
$this->getDI()
->getMail()
->send([$this->user->email => $this->user->username],
"Please confirm your email",
'confirmation',
['confirmUrl' => '/auth/confirm/' . $this->code . '/' . $this->user->email]
);
}
...
}
And a Mail library to get the corresponding template and send the email.
app\common\library\Mail.php
namespace MyApp\Mail;
use Phalcon\Mvc\User\Component;
use Swift_Message as Message;
use Swift_SmtpTransport as Smtp;
use Phalcon\Mvc\View;
class Mail extends Component
{
protected $transport;
public function getTemplate($name, $params)
{
$parameters = array_merge([
'publicUrl' => $this->config->application->publicUrl
], $params);
//If instead of returning getRender() I return "S" the dispatcher->forward works
//return ("S");
return $this->view->getRender('emailTemplates', $name, $parameters, function ($view) {
$view->setRenderLevel(View::LEVEL_LAYOUT);
});
return $view->getContent();
}
public function send($to, $subject, $name, $params)
{
$mailSettings = $this->config->mail;
$template = $this->getTemplate($name, $params);
// Create the message
$message = Message::newInstance()
->setSubject($subject)
->setTo($to)
->setFrom([
$mailSettings->fromEmail => $mailSettings->fromName
])
->setBody($template, 'text/html');
if (isset($mailSettings) && isset($mailSettings->smtp)) {
if (!$this->transport) {
$this->transport = Smtp::newInstance(
$mailSettings->smtp->server,
$mailSettings->smtp->port,
$mailSettings->smtp->security
)
->setUsername($mailSettings->smtp->username)
->setPassword($mailSettings->smtp->password);
}
// Create the Mailer using your created Transport
$mailer = \Swift_Mailer::newInstance($this->transport);
return $mailer->send($message);
}
}
}
After Signing Up, the user is recorded, the emailConfirmation record is created also in the database and the email is sended correctly.
But, instead of forwarding to other action, I get a blank page only with the flash message: "A confirmation mail has been sent to [email protected]". I get no Errors or Exceptions.
Does somebody know what could be happening?