Home >PHP Framework >YII >How to generate QR code in yii2

How to generate QR code in yii2

王林
王林Original
2020-02-17 16:25:353486browse

How to generate QR code in yii2

1、通过composer加载

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

或者添加

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

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

(推荐教程:yii框架

2、编辑/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',
    ),
 ),

3、在你的控制器中添加以下代码

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这个方法就能看到一张漂亮的二维码了。

更多编程相关内容,请关注php中文网编程教程栏目!

The above is the detailed content of How to generate QR code in yii2. 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