根据一张旧票 http://trac.symfony-project.org/ticket/7282(基于旧 symfony 论坛中的旧线程 http://oldforum.symfony-project.org/index.php/t/3778/) - 哪一点到最终来源 http://jnotes.jonasfischer.net/2009/10/symfony-link-tags-e-g-canonical/,您可以轻松创建一个帮助程序,将链接标记添加到您的页面(例如/lib/helper/CanonicalHelper.php
):
/**
* Add link tag to slot 'links'
*
* @param String $href [Absolute or internat URI]
* @param String $rel [value for 'rel' attribtue, e.g. 'canonical']
*
* @return void
*/
function add_link($href, $rel)
{
$slot = get_slot('links');
try {
$href = url_for($href, true);
$slot .= "\n<link rel=\"$rel\" href=\"$href\" />";
} catch (InvalidArgumentException $e) {
$slot .= "\n<!-- Could not add Link '$href': Only absolute or internal URIs allowed -->";
}
slot('links', $slot);
}
然后你可以在你的模板中调用它:
<?php add_link(
'http://newe4s.com/news/articles/view/033/job-news-and-information',
'canonical'
); ?>
最后,不要忘记将插槽添加到您的layout.php
:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Title</title>
<link rel="shortcut icon" href="/favicon.ico" />
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<?php include_slot('links'); ?>
</head>
如果您想从actions
,它也在博客文章中定义。
edit:
如果您创建一个名为的助手CanonicalHelper.php
当你想使用时不要忘记包含它add_link
功能:
<?php use_helper('Canonical') ?>