Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: Adding meta nofollow tag to layout from view

I want to be able to add a meta tag from a view (or controller if possible) in CakePHP

I have a page like /mycontroller/myview but when it is accessed with filters like:

/mycontroller/myview/page:2/max_price:500

Then I want to add meta no follow tags.

There is a meta method on the HtmlHelper class.

When I call it like this:

$this->Html->meta('keywords', 'test test test', array('inline'=>false));

It creates a meta tag like this:

<meta name="keywords" content="test test test" />

However, when I call it like this:

$this->Html->meta('robots', 'noindex, nofollow', array('inline'=>false));

I would naturally expect and want it to create this:

<meta name="robots" content="noindex, nofollow" />

Instead I get this though:

<link href="http://www.example.com/mycontroller/noindex, nofollow" type="application/rss+xml" rel="alternate" title="robots" />

What am I doing wrong?

like image 895
JD Isaacks Avatar asked Dec 16 '22 05:12

JD Isaacks


2 Answers

From the documentation page (last line)

If you want to add a custom meta tag then the first parameter should be set to an array. To output a robots noindex tag use the following code:

echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex'));

In your case:

echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow'),null,array('inline'=>false));

Hope this helps

like image 51
pleasedontbelong Avatar answered Feb 15 '23 17:02

pleasedontbelong


Here's a tweaked version of the code from this page. I've tested it, and it does work:

<?php
echo $this->Html->meta(
    array('name' => 'robots', 'content' => 'noindex, nofollow'),
    null,
    array('inline'=>false));
?>

Obviously you can write this in a single line -I just broke it down for ease of viewing here.

like image 25
Dave Avatar answered Feb 15 '23 16:02

Dave