Home >Backend Development >PHP Tutorial >Implementation code of Yii2.0 framework to generate QR code function

Implementation code of Yii2.0 framework to generate QR code function

黄舟
黄舟Original
2017-10-25 09:07:202309browse

通过composer安装:

1.下面的方法是通过composer加载


php composer.phar require "2amigos/yii2-qrcode-helper" "*"

或者添加


"2amigos/yii2-qrcode-helper" : "*"

到对应项目的composer.json文件中

通过归档文件安装:

不习惯用composer的用户可以点击此处本站下载二维码扩展

然后解压,将文件名修改为yii2-qrcode-helper放入vendor/2amigos/目录下面

编辑/vendor/yiisoft/extensions.php,添加以下代码到数组中


'2amigos/yii2-qrcode-helper' => 
   array (
    'name' => '2amigos/yii2-qrcode-helper',
    'version' => '1.0.2.0',
    'alias' => 
    array (
     '@dosamigos/qrcode' => $vendorDir . '/2amigos/yii2-qrcode-helper/src',
    ),
 ),

用法

在你的控制器中添加一下代码


use dosamigos\qrcode\QrCode;  //引入类
\\ ... 
public function actionQrcode() 
{ 
  return QrCode::png('http://www.yii-china.com');  //调用二维码生成方法
}
public function actionDemo()
{
  return $this->render('index');
}

在你的页面中添加以下代码 (demo/index)


<img src="<?= Url::to([&#39;index/qrcode&#39;])?>" />

访问demo这个方法,就能看到一张漂亮的二维码了

The above is the detailed content of Implementation code of Yii2.0 framework to generate QR code function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn