cakePHP 3.0 and bootstrap glyphicons

2019-03-04 10:57发布

问题:

I want to add glyphicons instead of text in my index, add, edit views.

This works in the index.ctp

<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil"></i>'), ['action' => 'edit', $user->user_id], array('escape' => false)) ?>

But when I do it for the delete action it shows me the glyphicon but it doesn't give me the 'Are you sure you want to delete the user?' anymore

<?= $this->Form->postLink(__('<i class="glyphicon glyphicon-minus"></i>'), ['action' => 'delete', $user->user_id], array('escape' => false), ['confirm' => __('Are you sure you want to delete {0}?', $user->username)]) ?>

In the view.ctp it breaks the code that comes after so the content that comes after isn't shown. (in this example that's the content after the glyphicon-pencil. The glyphicon-pencil itself isn't shown as well.

<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil'), ['action' => 'edit', $user->user_id], ['escape' => false]) ?> 

回答1:

Take a closer look at the arguments that you are passing, you are passing 4, where the method only accepts 3, ie the confirm option is not passed in the actual options argument.

Proper formatting helps a lot to spot such mistakes.

<?=
$this->Form->postLink(
    __('<i class="glyphicon glyphicon-minus"></i>'),
    [
        'action' => 'delete',
        $user->user_id
    ],
    [
        'escape' => false,
        'confirm' => __('Are you sure you want to delete {0}?', $user->username)
    ]
)
?>

And your FormHelper::link() example is missing a closing double quote for the <i> elements class attribute, as well as a closing tag for the element itself

'<i class="glyphicon glyphicon-pencil"></i>'

You need to pay more attention to the details, these problem where really really simple and easy to avoid.