5

I am using symfony 1.0.6.

In my site I have two URLs.

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

and

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

Now, all the new articles are using same layout and both above links get same data from database. Google is reporting duplication of contents since it is getting multiple URLs for same content. When I searched for a solution, I got that using "canonical" structure fixes this issue which require

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

to be added in head section of page

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

But problem here is, both are using same layout and based on article id (033 in above example), data is fetched and displayed. I cannot change or hard-code canonical href.

Are there any ways of adding link tag manually in action.class or in template file ?

unor
  • 92,415
  • 26
  • 211
  • 360
Manoj H
  • 145
  • 2
  • 12

3 Answers3

2

According to an old ticket (based on an old thread in the old symfony forum) - which point to the final source, you can esaily create an helper that add a link tag to your page (for example /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);
}

Then you can call it in your template:

<?php add_link(
  'http://newe4s.com/news/articles/view/033/job-news-and-information',
  'canonical'
); ?>

Finally, don't forget to add the slot in your 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>

If you want to add it from the actions, it is defined in the blog post too.

edit:

If you create an helper called CanonicalHelper.php don't forget to include it when you want to use add_link function:

<?php use_helper('Canonical') ?>
j0k
  • 22,600
  • 28
  • 79
  • 90
  • Hi I have created a helper under /lib/symfony/helper as CanonicaHelper.php and I have pasted the code containing add_link their. Then In template I called add_link() function and added include_slot('links') in layout.php Should I rename include_slot('links') to include_slot('Canonical').... I tried in both ways. It is not wortking – Manoj H Jun 11 '12 at 11:07
  • Hi I could not post my code properly in comment box. So I answered my own question. Please let me know if it right. – Manoj H Jun 11 '12 at 12:33
1

Symfony 1.0.11

Important part is slot('links') & end_slot() so whatever print in between will be assigned to slot similar to ob_start & ob_end()

function add_link($href, $rel)
   {
      slot('links');
      echo "\n<link rel=\"$rel\" href=\"$href\" />\n";
      end_slot();
   }
Community
  • 1
  • 1
Ani
  • 21
  • 4
0

Hi I am doing as below and please let me know If I am right or wrong.

In /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;
}
?>

In layout.php:

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

In Success file:

<?php use_helper('Canonical');?>
<?php echo add_link('nonsense', 'canonical');?>
Manoj H
  • 145
  • 2
  • 12
  • You put the helper in the wrong folder. Move the CanonicalHelper.php file in `/lib/helper/` (if the folder helper doesn't exist create it). – j0k Jun 11 '12 at 12:36
  • I am very sorry.. It was typing mistake.. :) it is in helper directory only. Apart from that is every think OK.. Many thanks... :) – Manoj H Jun 11 '12 at 13:07
  • hey... I am very sorry.. I am always thank full to you man... I just accepted my own answer and it removed your preference.. I am sorry again.. I accept your answer.. – Manoj H Jun 11 '12 at 13:23
  • no problem, and I think you can remove your answer since it was just a typing mistake ;) – j0k Jun 11 '12 at 13:32