Ryan_RCM
Ryan_RCM

Reputation: 3

CakePHP Creating External Links from Table Field

I am very new to CakePHP but am struggling to make pull a URL from a field within a table that makes an external link. It is currently using our domain then putting the web address after it.

Does anyone know how to force it so that uses an external wed address?

Here is the code I have used:

<a href="<?php echo $this->Html->url(array($result_array['Result']['results_video'])); ?>"><?php echo $this->Html->image('certificate_image.png', array('escape' => false)); ?></a>

Upvotes: 0

Views: 332

Answers (2)

T.J. Compton
T.J. Compton

Reputation: 405

HtmlHelper::url and Router::url take either an array of url components, ex:

$this->Html->url(array('controller' => 'page', 'action' => 'index'));
$this->Html->url($result_array['Result']['results_video']);

or a string, ex:

$this->Html->url('http://www.google.com');
$this->Html->url($variable['Model']['url']);

The only thing you can pass to the second argument of the url method is null or true, which prepends the full base url to a url generated from array components if true, so this wouldn't be where you'd pass that escape value, either.

Without knowing exactly what's in your $result_array['Result']['results_video'] value, it's hard to say exactly what you're doing wrong, but I'm guessing what you actually want is this, since contextually it looks like you've already got an array:

<?php
     echo $this->Html->link(
         $this->Html->image('certificate_image.png'),
         $result_array['Result']['results_video'],
         array('escape' => false)
     );
?>

(Note that there's almost never a need to echo values into an <a> tag when you can use HtmlHelper::link instead)

Upvotes: 0

floriank
floriank

Reputation: 25698

If the URL is already a string:

<a href="<?php echo $result_array['Result']['results_video']; ?>">
    <?php echo $this->Html->image('certificate_image.png'); ?>
</a>

If it's an array your code is right except that the escape option has to go into the link() method as 2nd arg and not in the image() method.

<a href="<?php $this->Html->url($result_array['Result']['results_video'], ['escape' => false]); ?>">
    <?php echo $this->Html->image('certificate_image.png'); ?>
</a>

If you're new to CakePHP I recommend you to look more often at the API documentation to see what args a method takes. http://api.cakephp.org/2.6/class-HtmlHelper.html. Most of the time there is an option array that can do what you want.

Upvotes: 2

Related Questions