我如何获得完整的url以包含在随Symfony2发送的新闻稿中?
我将使用Symfony2定期向许多用户发送新闻稿。我必须为那些在使用电子邮件客户端阅读HTML电子邮件时遇到问题的人提供一个永久链接 无论如何,假设我以这种方式发送时事通讯:我如何获得完整的url以包含在随Symfony2发送的新闻稿中?,url,symfony,permalinks,swiftmailer,Url,Symfony,Permalinks,Swiftmailer,我将使用Symfony2定期向许多用户发送新闻稿。我必须为那些在使用电子邮件客户端阅读HTML电子邮件时遇到问题的人提供一个永久链接 无论如何,假设我以这种方式发送时事通讯: // Assume success and create a new SentMessage to store and get permalink $sent = new SentMessage(); $sent->setRecipients(/* ... */); $sent->setSubject(/*
// 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(域+控制器+操作+slug)以将其包含在新电子邮件中
默认情况下,路由器将生成相对URL(例如/blog
)。到
生成一个绝对URL,只需将true
传递给
generate()
方法:
也许您的代码看起来像这样
Symfony2
Symfony3
小树枝
如果这是在细枝模板中,请使用url(“您的路线”)
而不是path(“您的路线”)
来获取绝对url。使用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
太神了学习Sf2的时间太长了,从来没有听说过完整的url生成。谢谢彼得,你总是帮助我们。太好了。谢谢
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
$url = $router->generate(
'slug_route_name',
array('slug' => $sent->getSlug()),
UrlGeneratorInterface::ABSOLUTE_URL // This guy right here
);
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