php 在 Yii2 中创建带有链接的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27401047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:21:14  来源:igfitidea点击:

Create button with link in Yii2

phpyiiyii2yii-widgets

提问by Wijnand

I am trying to setup a button with a link to a view. However yii\bootstrap\Buttondoes 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 urloption does not exist, is there an other way to fix this using Yii?

我正在尝试设置一个带有视图链接的按钮。不过yii\bootstrap\Button没有属性url。我宁愿使用 Yii 作为应该只使用平面 php。下面的代码将是理想的情况,但由于该url选项不存在,是否有其他方法可以使用 Yii 解决此问题?

echo Button::Widget([
    'label' => 'label',
    'options' => ['class' => 'btn btn-primary'],
    'url' => Url::toRoute(['/controller/action']),
]);

回答by soju

You could simply use Html::a():

您可以简单地使用Html::a()

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

Or create your own version of Buttonclass to handle this.

或者创建您自己的Button类版本来处理这个问题。

PS: you don't need Url::toRoute

PS:你不需要 Url::toRoute

回答by Kalpesh Desai

you can also pass parameter to url

您还可以将参数传递给 url

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

You can also render the html

您还可以呈现 html

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

回答by Federico Benedetti

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'),
                    ]
                )

回答by BartT

For me works:

对我来说有效:

<?= Html::button('Press me!', ArrayHelper::merge(['value'=>Url::to(['controller/action'])], ['additionalOptions'])); ?>

So, use ['value'=> Url::to(),]instead of ['url' => ...]

所以,使用['value'=> Url::to(),]代替['url' => ...]

回答by Mohan Prasad

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;']) ?>