Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create button with link in Yii2

I am trying to setup a button with a link to a view. However yii\bootstrap\Button does not have a property url. I would rather use Yii as supposed to just use flat out php. The code as below would be the ideal situation, but since the url option does not exist, is there an other way to fix this using Yii?

echo Button::Widget([
    'label' => 'label',
    'options' => ['class' => 'btn btn-primary'],
    'url' => Url::toRoute(['/controller/action']),
]);
like image 788
Wijnand Avatar asked Dec 10 '14 12:12

Wijnand


4 Answers

You could simply use Html::a() :

<?= Html::a('label', ['/controller/action'], ['class'=>'btn btn-primary']) ?>

Or create your own version of Button class to handle this.

PS: you don't need Url::toRoute

like image 66
soju Avatar answered Nov 13 '22 07:11

soju


you can also pass parameter to url

<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

You can also render the html

<?= Html::a('<span class="btn-label">Update</span>', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
like image 36
Kalpesh Desai Avatar answered Nov 13 '22 08:11

Kalpesh Desai


You can try this:

Html::button("<span class='glyphicon glyphicon-plus' aria-hidden='true'></span>",
                    ['class'=>'kv-action-btn',
                        'onclick'=>"window.location.href = '" . \Yii::$app->urlManager->createUrl(['/create','id'=>$model->id]) . "';",
                        'data-toggle'=>'tooltip',
                        'title'=>Yii::t('app', 'Create New Record'),
                    ]
                )
like image 3
Federico Benedetti Avatar answered Nov 13 '22 08:11

Federico Benedetti


If you want your label name or button to have translations

<?= Html::a(Yii::t('app','label'), ['/controller/action'], ['class'=>'pull-right', 'style' => 'padding-right:10px;']) ?>

If you want to add an icon for this link

 <?= Html::a("<i class=\"fa fa-icon\"></i> ".Yii::t('app','label'), ['/controller/action'], ['class'=>'pull-right', 'style' => 'padding-right:10px;']) ?>

if you want to pass parameters

 <?= Html::a(Yii::t('app','label'), ['/controller/action', id => $model->id], ['class'=>'pull-right', 'style' => 'padding-right:10px;']) ?>
like image 2
Mohan Prasad Avatar answered Nov 13 '22 08:11

Mohan Prasad