Cakephp format url with image and title - cakephp

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...............

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']
);

unable to get image with link in cakephp 2.x?

I tried to made image with link using FormHelper..in cakephp. Below are my script:
<?php
echo $this->Html->link($this->Html->image('images/view-more-arrow.png') . ' ' . __('View More'),array('controller' => 'zones', 'action' => 'index'), array('escape' => false));
?>
Output:
<img src="/project_folder/trunk/img/images/view-more-arrow.png" alt=""> View More
Expect:
<img src="/project_folder/trunk/images/view-more-arrow.png" alt=""> View More
My image directory path is project_folder/app/webroot/images. I don't know why its take img/ automatic.
Thank you in Advance..
I refereed this link:
Cakephp html link with image + text, without using css
You can use the slash at the beginning of the path because is relative to the app/webroot directory:
echo $this->Html->link($this->Html->image('/images/view-more-arrow.png') . ' ' . __('View More'),array('controller' => 'zones', 'action' => 'index'), array('escape' => false));
You can also try this, it works perfectly for me.
$hd = $this->Html->image('hd.jpg',array('alt'=>'harley Davidson', 'border'=>'0', 'width'=>'450', 'height'=>'250'));
echo $this->Html->link($hd,array('controller'=>'Posts', 'action'=>'add'), array('escape'=>false));
Here in $hd, I define the path for the image and then I use this for to make link.

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)
); ?>

CakePHP Canonical Tag with html helper

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') ?>

Resources