cakePHP formHelper postLink图像

时间:2011-11-12 15:38:23

标签: php cakephp

我想使用cakePHP的formHelper :: postLink函数输出一个带有超链接的图像,而不仅仅是文本超链接。

有谁知道怎么做?我尝试了很多东西,却无法让它发挥作用。

<?php echo $this->Form->postLink(
    'Delete',
    array('action' => 'delete', $country['Country']['id']),
    array('confirm' => __('Are you sure you want to delete ').$country['Country']['name'].'?')
)?>

因此,我想要显示图像,而不是“删除”。

6 个答案:

答案 0 :(得分:9)

这对我有用。

echo $this->Form->postLink(
  $this->Html->image('icn_trash.png', array('alt' => __('Effacer'))), //le image
  array('action' => 'delete', $artist['Artist']['id']), //le url
  array('escape' => false), //le escape
  __('Êtes-vous sûr de vouloir effacer artiste #%s?', $artist['Artist']['id']) //le confirm
); //le voila

答案 1 :(得分:4)

试试这个:

echo $this->Form->postLink(
    $this->Html->image('delete.png',
       array("alt" => __('Delete'), "title" => __('Delete'))), 
    array('action' => 'delete', $items['Item']['id']), 
    array('escape' => false, 'confirm' => __('Are you sure?')) 
);

答案 2 :(得分:2)

如果我理解你的问题,我认为你不想使用$this->Form->postLink

我认为此页面正是您所追求的:http://book.cakephp.org/view/1441/image

这使用$this->Html->image创建图像,然后您可以通过URL作为参数之一来指定周围的锚链接。

答案 3 :(得分:1)

您可以在link元素中包装图像,但是您需要将escape选项设置为false,如下所示:

echo $this->Html->link(
    $this->Html->image('your_image_here.jpg', array(
        'alt' => 'Alternative Text for your image',
        'title' => 'Optional tooltip text for your image'
    ),
    array(
        'controller' => 'YourController',
        'action' => 'someAction'
    ),
    array(
        'escape' => false // Add this to avoid Cake from printing the img HTML code instead of the actual image
    )
);

这应该可以解决问题。

答案 4 :(得分:0)

echo $this->Html->link(
    $this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
    array(
        'controller' => 'recipes',
        'action' => 'view',
        'id' => 6,
        'comments' => false
    )
)

答案 5 :(得分:-4)

function delete_image(){

    if ($this->Session->read('Auth.User.id')) {

        $this->User->id = $this->Session->read('Auth.User.id');
        $this->User->updateAll(
                            array('User.image' => "''"),
                            array('User.id' => $this->User->id)
                            );

        $this->Session->setFlash('The image has been deleted.');
        $this->redirect(array('action' => 'profile'));      
        }
}