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

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.

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 Open to New Tab on Click

I have a function in my application which the users can upload files to the webserver. Then these uploaded files will appear in another page wherein another type of users can click on the link. Once the link is clicked, a new tab will open and the file will be shown.
But I can't seem to do it. Using the 'target' => '_blank' is not working, or I may have put it on the wrong part of the code.
In my case, when you click on the link, the file will load on the same tab.
Here's my code:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
'target' => '_blank',
$staff_uploads['StaffUpload']['iduploads']
)
);
?>
Thank you in advance!
The correct code is:
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array(
'controller' => 'websites',
'action' => 'view',
$staff_uploads['StaffUpload']['iduploads']
), array('target' => '_blank')
);
?>
And do read the documentation as burzum has suggested.
Read the documentation.
HTML attribute options go into the 3rd argument of the link() method, not the second which is the URL as string or array.
Problems like this can be simply resolved by using the documentation.

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.

How can I get full link to an image in the img tag through the Html helper (image method)?

I have this code snippet in an email layout:
<div>
<?php echo $this->Html->image('Layouts/default/Logo.png', array(
'alt' => 'Setin SRL',
'url' => $this->Html->url(array('action' => 'index'), true)
)); ?>
</div>
However this is not ok. I can get the FULL url to the website through url(array('action' => 'index'), true)
But I can't find any paramater that I can set to true for image too. Any suggestion or workaround on how can I do this?
Edit 1:
Basically, I need this link: Layouts/default/Logo.png to become http://www.something.com/Layouts/default/Logo.png in the img src
i always use
$this->Html->link($this->Html->image(...), $this->Html->url(array('action' => 'index'), true), array('escape'=>false));
I think this is what you are looking for.
<?
echo $this->Html->image(Router::url('/') . 'Layouts/default/Logo.png');
?>
What you are looking for is probably the HtmlHelper::assetUrl function that is used in the implementation of image method:
public function image($path, $options = array()) {
$path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
...
$img = '<img src="http://some-img-link" alt="some-img-alt"/>';
$src = preg_match('/<img src=\"(.*?)\">/', $img);
echo $src;
I want to get the src value from the img tag and maybe the alt value

Resources