2012-06-08 155 views
5

我正在使用symfony 1.0.6。如何添加規範標籤從相同鏈接派生的頁面?

在我的網站我有兩個網址。

http://newe4s.com/news/articles/view/033/job-news-and-information 

http://newe4s.com/news/articles/view/033/job-news-and-information/graduate/Connections-help-graduates-get-jobs 

現在,所有的新文章使用相同的佈局和兩個以上的鏈接從數據庫中獲得相同的數據。 Google正在報告內容重複,因爲它獲取的是同一內容的多個網址。 當我尋找一個解決方案,我得到了使用「規範」的結構解決這個問題需要

<link rel="canonical" href="http://newe4s.com/news/articles/view/033/job-news-and-information /> 

在頁面

http://newe4s.com/news/articles/view/033/job-news-and-information/graduate/Connections-help-graduates-get-jobs 

的頭部分,但問題被加入這裏,無論是使用相同的佈局和基於文章ID(上例中的033),數據被提取並顯示。 我無法更改或硬編碼規範href。

有沒有任何方法在action.class或模板文件中手動添加鏈接標記?

回答

2

根據an old ticket(基於old thread in the old symfony forum) - 這點to the final source,您可以esaily創建一個鏈接標記添加到您的網頁(例如/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中添加它,它也在博客文章中定義。

編輯:

如果你創建了一個名爲CanonicalHelper.php當你想使用add_link功能不要忘記包括一個幫手:

<?php use_helper('Canonical') ?> 
+0

嗨無論打印我已經創建了/ lib下一個幫手/ symfony/helper作爲CanonicaHelper.php,我粘貼了包含add_link的代碼。 然後在模板我layout.php中稱爲add_link()函數,並添加include_slot(「鏈接」) 我應該重新命名include_slot(「鏈接」),以include_slot(「規範」)......我在這兩個方面嘗試。它不是wortking –

+1

我已經編輯我的回答(你需要使用'use_helper') – j0k

+0

您好我無法正常發佈在評論框我的代碼。所以我回答了我自己的問題。請讓我知道是否正確。 –

0

嗨,我下面做什麼,請讓我知道如果我是對還是錯。

在/lib/symfony/CanonicalHelper.php

<?php 
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 -->"; 
} 
return $slot; 
} 
?> 

在layout.php中:

<?php include_slot('links'); ?> 

在成功的文件:

<?php use_helper('Canonical');?> 
<?php echo add_link('nonsense', 'canonical');?> 
+0

你把幫手放在錯誤的文件夾中。將CanonicalHelper.php文件移到'/ lib/helper /'中(如果文件夾幫助程序不存在,則創建它)。 – j0k

+0

我很抱歉..這是打字錯誤.. :)它只在助手目錄中。除此之外,是每一個想行.. 非常感謝... :) –

+0

哎...我很抱歉..我永遠感謝全給你的男人...我只是接受了我自己的答案,並刪除你的喜好..我不好意思再......我接受你的答案.. –

1

Symfony的1.0。11

重要的部分是槽(「鏈接」)& end_slot(),所以在兩者之間將被分配到插槽類似ob_start & OB_END()

function add_link($href, $rel) 
    { 
     slot('links'); 
     echo "\n<link rel=\"$rel\" href=\"$href\" />\n"; 
     end_slot(); 
    } 
相關問題