CakePHP: adding a meta nofollow tag to a layout from a view

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

I have a page like /mycontroller/myview , but when I access it, filters like:

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

Then I want to add meta no follow tags.

There is a meta method in the HtmlHelper class.

When I call it this way:

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

It creates a meta tag similar to this:

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

However, when I call it this way:

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

Naturally, I would expect and want to create this:

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

Instead, I get this:

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

What am I doing wrong?

+4
source share
3 answers

On the documentation page (last line)

If you want to add your own meta tag, then the first parameter must be set to an array. To display noindex robot tags, 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

+8
source

Here is a modified version of the code for this page . I tested it and it works:

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

Obviously, you can write this on one line - I just broke it for easy viewing here.

+3
source

You can set the variables from the view to the layout in the same way as you set from the controller to view using $this->set() , I would have this setting:

 // View if($condition) { $this->set('nofollow', true); } // Layout (in <head>) if(isset($nofollow) && $nofollow) { echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow')); } 

You now have a short 1-liner to add the nofollow directive from any view file.

+1
source

Source: https://habr.com/ru/post/1389095/


All Articles