gre*_*emo 20 url permalinks swiftmailer symfony
我将使用Symfony2定期向许多用户发送简报.我要为那些在使用电子邮件客户端阅读时遇到问题的人提供HTML电子邮件的固定链接.
无论如何,假设我以这种方式发送简报:
// Assume success and create a new SentMessage to store and get permalink
$sent = new SentMessage();
$sent->setRecipients(/* ... */);
$sent->setSubject(/* ... */);
$sent->setContent(/* ... */);
// Get the slug for the new sent message
$slug = $sent->getSlug(); // Like latest-product-offers-546343
// Construct the full URL
// e.g. http://mydomain.com/newsletter/view/latest-product-offers-546343
// Actually send a new email
$mailer->send(/* .. */);
Run Code Online (Sandbox Code Playgroud)
如何构建完整的URL(域+控制器+动作+ slug)以将其包含在新电子邮件中?
Pet*_*ley 51
当然,有了路由器
默认情况下,路由器将生成相对URL(例如
/blog).要生成绝对URL,只需传递true给方法的第三个参数generate():
也许你的代码看起来像这样
$url = $router->generate(
'slug_route_name',
array('slug' => $sent->getSlug()),
true // This guy right here
);
Run Code Online (Sandbox Code Playgroud)
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
$url = $router->generate(
'slug_route_name',
array('slug' => $sent->getSlug()),
UrlGeneratorInterface::ABSOLUTE_URL // This guy right here
);
Run Code Online (Sandbox Code Playgroud)
ArG*_*rGh 15
使用Symfony 3进行更新:
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
$this->generateUrl('blog_show', array('slug' => 'my-blog-post'), UrlGeneratorInterface::ABSOLUTE_URL);
// http://www.example.com/blog/my-blog-post
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14446 次 |
| 最近记录: |