CakePHP Absolute Image URLs

15,927

Solution 1

In CakePHP 1.x the method is:

$this->Html->image($this->Html->url('/path_to_image/image.png',true));

In CakePHP 2.x the method is:

$this->Html->image('/path_to_image/image.png', array('fullBase' => true));

Solution 2

The correct way to do this is in CakePHP 2.x:

<?php
echo $this->Html->image("logo.png", array('fullBase' => true));

Code pulled direct from the cookbook: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

Solution 3

$html->image('http://example.com/path_to_image/image.png'); should do the trick.

Share:
15,927
andygeers
Author by

andygeers

Updated on July 01, 2022

Comments

  • andygeers
    andygeers almost 2 years

    I wish to use the $html->image(...) helper function in CakePHP to output images, but I need it to produce an img tag using an absolute rather than relative URL (the resulting HTML will be downloaded and emailed round in a company newsletter). Is this possible?

    It's not documented, but I notice from looking at the source code that the image function can take an array as its first argument. It's not entirely clear to me how to get this working though - a naive attempt to do it this way produces image URLs relative to the current page rather than within the webroot/img folder.

  • Matthew Groves
    Matthew Groves almost 15 years
    That should be CakePHP's motto :)
  • JD Isaacks
    JD Isaacks over 11 years
    @Jleagle I updated my answer to specify the correct way in each version. (1.3 was the current version when I originally posted this answer)
  • Vael Victus
    Vael Victus over 10 years
    In Cake's case, it is expressly not.
  • Matt
    Matt about 10 years
    What if I move my site to be on example2.com?