Welcome to the Cookbook

loading...

このセクションには保留されている変更があります. More information about translations

link(string $title, mixed $url = null, array $htmlAttributes = array(), string $confirmMessage = false, boolean $escapeTitle = true)

General purpose method for creating HTML links. Use $htmlAttributes to specify attributes for the element.

<?php echo $html->link('Enter', '/pages/home', array('class'=>'button','target'=>'_blank')); ?>
  1. <?php echo $html->link('Enter', '/pages/home', array('class'=>'button','target'=>'_blank')); ?>

Will output:

  
<a href="/ja/pages/home" class="button" target="_blank">Enter</a>

Specify $confirmMessage to display a javascript confirm() dialog.

<?php echo $html->link(
    'Delete',
    array('controller'=>'recipes', 'action'=>'delete', 6),
    array(),
    "Are you sure you wish to delete this recipe?"
);?>
  1. <?php echo $html->link(
  2. 'Delete',
  3. array('controller'=>'recipes', 'action'=>'delete', 6),
  4. array(),
  5. "Are you sure you wish to delete this recipe?"
  6. );?>

Will output:

  
<a href="/ja/recipes/delete/6" onclick="return confirm('Are you sure you wish to delete this recipe?');">Delete</a>

Query strings can also be created with link().

<?php echo $html->link('View image', array(
    'controller' => 'images',
    'action' => 'view',
    1,
    '?' => array( 'height' => 400, 'width' => 500))
);
  1. <?php echo $html->link('View image', array(
  2. 'controller' => 'images',
  3. 'action' => 'view',
  4. 1,
  5. '?' => array( 'height' => 400, 'width' => 500))
  6. );

Will output:

  
<a href="/ja/images/view/1?height=400&width=500">View image</a>

HTML special characters in $title will be converted to HTML entities. To disable this conversion, set the escape option to false in the $htmlAttributes, or set $escapeTitle to false.

<?php 
echo $html->link(
    $html->image("recipes/6.jpg", array("alt" => "Brownies")),
    "recipes/view/6",
    array('escape'=>false)
);

echo $html->link(
    $html->image("recipes/6.jpg", array("alt" => "Brownies")),
    "recipes/view/6",
    null, null, false
);
?>
  1. <?php
  2. echo $html->link(
  3. $html->image("recipes/6.jpg", array("alt" => "Brownies")),
  4. "recipes/view/6",
  5. array('escape'=>false)
  6. );
  7. echo $html->link(
  8. $html->image("recipes/6.jpg", array("alt" => "Brownies")),
  9. "recipes/view/6",
  10. null, null, false
  11. );
  12. ?>

Both will output:

<a href="/ja/recipes/view/6">
    <img src="/img/recipes/6.jpg" alt="Brownies" />
</a>

Also check HtmlHelper::url method for more examples of different types of urls.