如何获取完整的URL,以将其包含在Symfony2发送的新闻通讯中? [英] How can i get full url to include in newsletter sent with Symfony2?

查看:76
本文介绍了如何获取完整的URL,以将其包含在Symfony2发送的新闻通讯中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用Symfony2定期向许多用户发送新闻通讯.对于那些在使用电子邮件客户端读取邮件时遇到问题的人,我必须添加HTML电子邮件的永久链接.

I'm going to use Symfony2 to sent periodically a newsletter to many users. I've to include a permalink to the HTML email for those who experience problems in reading them with an email client.

无论如何,假设我以这种方式发送时事通讯:

Anyway, assuming that i'm sending the newsletter this way:

// 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(/* .. */);

我如何构建完整的URL(域+控制器+操作+子句)以将其包含在新电子邮件中?

How can i construct the full URL (domain + controller + action + slug) to include it in a new email?

推荐答案

使用

默认情况下,路由器将生成相对URL(例如/blog).到 生成绝对URL,只需将true传递给的第三个参数 generate()方法:

By default, the router will generate relative URLs (e.g. /blog). To generate an absolute URL, simply pass true to the third argument of the generate() method:

也许您的代码可能看起来像这样

Perhaps your code might look like this

$url = $router->generate(
    'slug_route_name',
    array('slug' => $sent->getSlug()),
    true // This guy right here
);

Symfony3

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

$url = $router->generate(
    'slug_route_name',
    array('slug' => $sent->getSlug()),
    UrlGeneratorInterface::ABSOLUTE_URL // This guy right here
);

这篇关于如何获取完整的URL,以将其包含在Symfony2发送的新闻通讯中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆