CakePHP Canonical Tag with html helper - cakephp

How can I create this using the html helper? (with inline=false so i can specify it on a per-view basis)
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish" />
Can't seem to find anything on this, apart from a patch that doesn't work.

Found this in CakePHP bugtracking site : http://cakephp.lighthouseapp.com/projects/42648/tickets/1063-support-for-custom-meta-tag-elements-in-htmlhelper
Apparently you can use
echo $this->Html->meta('canonical', 'http:://example.com', array('rel'=>'canonical', 'type'=>null, 'title'=>null));
//outputs <link href="http:://example.com" rel="canonical" />

It seems my friend just told me that I told him how to do this a few months back, problem solved...
<?php echo $this->Html->meta('canonical',
'http://www.example.com/product.php?item=swedish-fish',
array('rel'=>'canonical', 'type'=>null, 'title'=>null, 'inline' => false)
);?>

If you're looking for something that automatically outputs the current url into a canonical tag, you can use the $this->Html->url(null, true); or $this->here; within the Cakephp html helper.
<?php echo $this->Html->meta('canonical', $this->Html->url(null, true), array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>
Or
<?php echo $this->Html->meta('canonical', $this->here, array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>
WARNING:
I have heard of some cases where $this->here has issues on local dev environments.

In CakePHP 2:
echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'inline' => false));
In CakePHP 3:
echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'block' => true));
Note that the main difference between versions is that CakePHP 2 uses 'inline' => false whereas CakePHP 3 uses 'block' => true to place these within the document <head> tags.

In CakePHP 4:
In your view (es: Articles/view.php) add this:
<?php $this->Html->meta(
'canonical',
Router::url(['controller' => 'Articles', 'action' => 'view', $article->slug], true),
[
'block' => true
]
);
?>
Then you print it in your layout/default.ctp with this instruction
<?= $this->fetch('meta') ?>

Related

How to use class in anchor tag and img tag

<img src="images/abc.jpg" class="img-style row2">.
I have to use above code in cakephp3. I have tried by html helper but there is some syntax error with my code.
Here is my code,
<?php echo $this->Html->link('".$this->Html->image('abc.jpg', ['class' => 'img-style row2'])."', $this->Html->image('xyz.jpg', ['class' => 'swipebox'])); ?>
Please help anybody regarding this.
Use image method into link method then use 'escape' => false, see example
echo $this->Html->link(
$this->Html->image('img.jpg',['class'=>'img-class','alt' => 'img',]),
['controller'=>'', 'action'=>''],
['escape' => false,'class'=>'link-class']
);

Cakephp ajax form won't load the right view

I'm just trying to call a function from a form submision (which is also call from ajax).
I want to call the add method but it's the index which always called.
My ajax view with the ajax submission form :
<?php $count = count($files); ?>
<div id="message"></div>
<i><?= __('Nombre de fichiers liƩs : ') . $count ?></i>
<?= $this->Form->create('FilesManager', array('enctype' => 'multipart/form-data', 'url' => array('action' => 'add'))); ?>
<?= $this->Form->input('file', array('type' => 'file', 'label' => false, 'class' => 'form-control')); ?>
<?= $this->Js->submit('Envoyer', array('update' => '#message', 'div' => false, 'type' => 'json', 'async' => false)); ?>
<?= $this->Js->writeBuffer(); ?>
i've also tried :
<?= $this->Form->create(null, array('enctype' => 'multipart/form-data', 'url' => array('controller' => 'FilesManagers', 'action' => 'add'))); ?>
<?= $this->Form->create('FilesManager/add', array('enctype' => 'multipart/form-data')); ?>
EDIT
$this->Form->create('FilesManager', array('action' => 'add'))
Don't work for me but the form generated look like :
<form action="/agralis/files_managers/add" enctype="multipart/form-data" id="FilesManagerIndexForm" method="post" accept-charset="utf-8">...<input id="submit-838644811" type="submit" value="Envoyer"><script type="text/javascript">
//<![CDATA[
$("#submit-838644811").bind("click", function (event) {$.ajax({async:false, data:$("#submit-838644811").closest("form").serialize(), dataType:"html", success:function (data, textStatus) {$("#message").html(data);}, type:"post", url:"\/agralis\/FilesManagers"});
return false;});
//]]>
</script></form>
I can see that form action look good (when i copy/paste the action in the browser url he found the method) but the url called from the ajax submit button is wrong ! How can I change that ?
The helpers are working independently
The Js helper is not aware of the Form helper, they don't interact with each other, and so the form URL is unknown to the Js helper.
The solution for that problem is rather simple, just look at the docs:
http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::submit
The submit() method accepts a url option where you can specify a URL in case the default one that is based on the current request doesn't fit you.
$this->Js->submit('Envoyer', array(
'url' => array('action' => 'add')
// ...
));
File uploads are not supported
The next problem that you'll encounter is that the file upload doesn't work. That's not part of the question, but I'll broach the subject.
The Js helper doesn't support generating the required JavaScript to handle file uploads, it would need to make use of the XMLHttpRequest Level 2 (xhr2) functionalities that older browsers are lacking.
You'll have to write custom JavaScript to handle that. Explanations and tutorials regarding that can be found all over the web.

Cakephp format url with image and title

i'm tring to change my link into a CakePhp format link. I want aso to add the class "ajax" in my link.
<li><?php echo $this->Html->link($this->Html->image('images/home.png'),'Accueil', array ('action'=>'index.php'),array('class'=>'ajax')); ?>
My original link :
<li><a href="index.php" id="visited"><span class="home">
<img src="<?php echo $this->webroot; ?>images/home.png" alt=""></span>Accueil</a></li>
Thanks
You can do this as mark said and additionally if you want to add the span tag,
<li><?php echo $this->Html->link($this->Html->tag('span', $this->Html->image('images/home.png'), array('class' => 'home', 'escape' => false)).' Accueil', array ('action'=>'index.php'),array('class'=>'ajax', 'escape' => false)); ?></li>
Hope it helps.
echo $this->Html->tag('li',$this->Html->link(
$this->Html->tag('span',
$this->Html->Image('images/home.png',array(
'alt'=>'','height'=>'100%','width'=>'100%'
)).'Accueil',
array('class'=>'home')
),
array(
'controller' =>'','action' => 'index.php'
),
array(
'class' = 'ajax'
)
)
)
I have written this for you to learning link & tag building in cakephp....i thinks it helpful for you...............

Assign html attributes to url link in CakePHP's HtmlHelper's image

I am trying to find a way to set attributes to the link URL generated by the image method of CakePHP's HtmlHelper. I'm using CakePHP 2.2.1
For example, the following code:
echo $this->Html->image("recipes/6.jpg", array(
"alt" => "Brownies",
'url' => array('controller' => 'recipes', 'action' => 'view', 6)
));
generates:
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
How can I add attributes to the href tag. Say, for example, class='picture' to look like:
<a href="/recipes/view/6" class='picture'>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
You cannot add HTML attributes to the Anchor tag via the Html->image method - the way to do it is to put the Html->image within the Html->link method like so:
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array('alt' => 'Brownies')),
array('controller' => 'recipes', 'action' => 'view', 6, array('escape'=>false', 'class'=>'picture')
);
You must also include the 'escpape'=>false - otherwise your <img...> will be escaped, and it will show up like <img ... >
If you want to add any attribute to HTML anchor tag using HtmlHelper, then you can use it via:
<?php echo $this->Html->link(
$this->Html->image("loading.gif", array('alt' => 'Brownies', 'border' => '0')),
array('controller' => 'recipes', 'action' => 'view', 6), array('class' => 'picture', 'escape' => false));
In case you are still looking for an answer, what you have to do is instead of using $this->Html->image with the optional URL attribute, you need to use $this->Html->link with the optional escape = false attribute where the title of the link is your image, utilizing $this->Html->image. What escape = false does is unescape special characters in the title of the link allowing you to use an image or other html element.
Here is an example
echo $this->Html->link(
$this->Html->image($image['Image']['file'], 'class' => 'image', 'alt' => $image['Image']['title'])),
'path/to/image', // or an array('controller' => 'mycontroller', 'action' => 'myaction')
array('escape' => false));
You can add more image attributes such as the class and alt I have listed as well as more link attribute such as the escape I have listed.

span in link with class

I was messing around with the cakePHP link tag...
And, the span is located inside the link.
I know about escape => false, but seems to me, this doesn't really work.
The php part is embedded within the 'li' as below:
<?php echo $this->Html->link($html->tag('span','Hello World'),
array('controller'=>test,
'action'=>index),
array('class' => 'class_b'),
array('escape' => false)
)
?>
My problem here is, the 'span' tag isn't 'eliminated' from the view. What am I doing wrong?
Thanks.
It's rather simple
<?
echo $this->Html->link($this->Html->tag('span',__('News',true)),array('controller'=>'news','action'=>'index'),array('escape'=>false,'class'=>'news'));
?>
you just need to add third parameter to Html link escape=>false
I think this is what you might be looking for, then:
<?php
echo $this->Html->link(
$this->Html->tag('span', 'Hello World.', array('class' => 'class_b')),
array(
'controller' => 'test',
'action' => 'index'
)
);
?>
Found in this reference (near the bottom of the page):
http://book.cakephp.org/1.3/view/1442/link
I was searching answer and for me works this piece of code:
<?php echo $this->Html->link(
$this->Html->tag('span', 'Hello world', array('class' => 'class_a')),
array('controller' => 'test', 'action' => 'index'),
array('escape' => FALSE)
); ?>

Resources