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

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.

Related

how to link to a specific point on a page with cakephp

I'm trying to get a link in cakephp to point to a specific place on another page but what I imagined would work doesn't.
I'm using
<a name="Telstra"></a>
Telstra
Can anyone tell me the correct way?
You need to use the built-in link function of CakePHP. try to use this code.
<?php echo $this->Html->link('NameOfLink', array('controller' => 'ControllerName', 'action' => 'FunctionName/#Telstra')); ?>
See this url from cakephp docs:
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
For creating link only
echo $this->Html->url(array(
"controller" => "posts",
"action" => "search",
"#" => "first"
));
and in your case for creating the link with anchor tag,
echo $this->Html->link('Telestra',array(
"controller" => "sponsors",
"action" => "index",
"#" => "Telstra"
));
If you want to load on a specific part of the page you must use ID on the element of your target rather than Name.
Example:
//Your target element on a page
<a name="Telstra"></a>
//URL that will redirect you to your target element.
Telstra
Or might as well code it the cakePhp style. :)
//URL
<?php echo $this->Html->link('Telstra', array('controller' => 'YourController', 'action' => 'YourFUnction', '#TargetElement'));
//Target Element
<a name="Telstra"></a>
Good Luck!
you need put a route in the folde of configuration /app/config/routes.php:
Router::connect('/index', array('controller' => 'yourController', 'action' => 'index'));
or
Router::connect('/index/*', array('controller' => 'yourController', 'action' => 'index'));
I think the parameter you're meaning to put is 'id' and not 'name' and I think you can just put #idName instead of the whole link.
For example:
<a id="Telstra"></a>
Telstra
For more information you can check here: http://www.w3schools.com/html/html_links.asp

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

How can i remove /index in URL in routing in cakePHP?

I want to remove /index in URL.
I have already tried the following in routes.php file :
Router::connect('/Home/:index', array('controller' => 'Homes'));
and :
Router::connect('/Home/', array('controller' => 'Homes','action'=>'index'));
Try
Router::connect('/Home', array('controller' => 'Homes','action'=>'index'));
Use this code for create link
<?php
echo $this->Html->link(__('YOUR_TEXT'),array('controller' => 'Homes', 'action' => 'index',array('class' => "", 'id' => ""));
?>
If you are creating link with this code then if is there any change made in routes then cakephp will update link according to that

CakePHP & Twitter Bootstrap: CSS icons in a button

I'd like to insert a icon of Twitter Bootstrap into this specific button.
The previous mentioned method to add 'escape' => false (here: CakePHP & Twitter Bootstrap CSS button icons) does not work for me.
I tested it succesfully with a simple html-bootstrap-code - but with cakePHP-specific code it does not work.
echo $this->Form->postLink(__('set User'),
array('controller' => 'institutions', 'action' => 'setAssignee', $user['User'] ['institution_id'], $user['User']['id']),
array('class' => 'btn btn-warning icon-plus'),
__('Are you sure?', $user['User']['last_name'], $institution['Institution']['name']));
Won't work, the icon-plus class will be overridden by the background applied to btn.
To get this to work, use an <i> like this:
echo $this->Form->postLink('<i class="icon-plus"></i> ' . __('set User'),
array('controller' => 'institutions', 'action' => 'setAssignee', $user['User'] ['institution_id'], $user['User']['id']),
array('class' => 'btn btn-warning', 'escape'=>false),
__('Are you sure?', $user['User']['last_name'], $institution['Institution']['name']));

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