QR(快速反应)码是可扫描的黑色和白色方块,可存储数据。
它们通常被用来存储信息, 重定向用户到一个网站, 下载文件, 支付等.
在本教程中,我展示了如何在Laravel 8中使用简单的qrcode包生成QR码。

内容
1.安 装包
使用composer安装软件包 -
composer require simplesoftwareio/simple-qrcode
2.更 新app.php
- 打开
config/app.php文件。 - 添加以下内容
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class在'providers'-
'providers' => [
....
....
....
SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
];
- 添加以下内容
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class在'aliases'-
'aliases' => [
....
....
....
'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
];
3.路 线
- 打开
routes/web.php文件。 - 定义一个路由 -
- / 加载索引视图。
已完成的代码
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PageController;
Route::get('/', [PageController::class, 'index']);
4.控 制器
- 创建
PagesController控制器:
php artisan make:controller PagesController
- 打开
app/Http/Controllers/PagesController.php文件。 - 导入QrCode包-
use SimpleSoftwareIO\QrCode\Facades\QrCode; - 创建一个方法
- index() 生成一个简单的QR码,其中包含一些文本并分配给
$data['qrcode']。
- index() 生成一个简单的QR码,其中包含一些文本并分配给
生成带有文本的QR码,并存储在images/ 文件夹中供下载。加载index 视图并传递给$data :
$data['qrcode'] = QrCode::generate('Welcome to Makitweb');
// Store QR code
QrCode::generate('Welcome to Makitweb', public_path('images/qrcode.svg') );
完成代码
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class PageController extends Controller
{
public function index(){
// QR code with text
$data['qrcode'] = QrCode::generate('Welcome to Makitweb');
// Store QR code for download
QrCode::generate('Welcome to Makitweb', public_path('images/qrcode.svg') );
return view('index',$data);
}
}
5.视 图
在resources/views/ 文件夹中创建index.blade.php 文件。
显示在控制器中生成的QR码-
{!! $qrcode !!}
带有文本的QR码 -
{!! QrCode::generate('Welcome to Makitweb'); !!}
在generate() 中传递文本。
带有URL的QR码 - 在
{!! QrCode::generate('https://makitweb.com'); !!}
在generate() 中传递URL。
改变QR码的颜色 -
QrCode::color(224, 224, 224)->backgroundColor(102, 0, 204)->generate('Welcome to Makitweb');
color() 通过RGB颜色代码来改变QR码的颜色。
backgroundColor() 传递RGB颜色代码来改变QR的背景颜色。
改变大小 -
QrCode::size(200)->generate('Welcome to Makitweb');
默认情况下,QR码的大小是100px,要改变它请使用size() 。
改变格式 -
QrCode::format('png')->generate('Welcome to Makitweb');
二维码的默认格式是SVG,要改变它,请将png 改为format() 。
注意确保安装了
imagick扩展,否则会显示错误。
下载QR码-
将QR码保存到images/qrcode.svg ,使用------
// Store QR code
QrCode::generate('Welcome to Makitweb', public_path('images/qrcode.svg') );
在锚点标签中传递文件路径-
<a href="{{ asset('images/qrcode.svg') }}" download>Download</a>
完成代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Generate QR Code Using Simple QRcode In Laravel 8</title>
</head>
<body>
<table border="1" style="border-collapse: collapse;">
<tr>
<td>
QR with text <br>
Generated from controller
</td>
<td>
{!! $qrcode !!}
</td>
</tr>
<tr>
<td>
QR with text
</td>
<td>
{!! QrCode::generate('Welcome to Makitweb'); !!}
</td>
</tr>
<tr>
<td>
QR with URL
</td>
<td>
{!! QrCode::generate('https://makitweb.com'); !!}
</td>
</tr>
<tr>
<td>
Change QR color
</td>
<td>
{!! QrCode::color(224, 224, 224)->backgroundColor(102, 0, 204)->generate('Welcome to Makitweb'); !!}
</td>
</tr>
<tr>
<td>
Change default size to 200px
</td>
<td>
{!! QrCode::size(200)->generate('Welcome to Makitweb'); !!}
</td>
</tr>
<tr>
<td>
PNG format
</td>
<td>
{!! QrCode::format('png')->generate('Welcome to Makitweb'); !!}
</td>
</tr>
<tr>
<td>Download QR code</td>
<td>
<a href="{{ asset('images/qrcode.svg') }}" download>Download</a>
</td>
</tr>
</table>
</body>
</html>
6.演 示
7.总 结
你可以从控制器中生成一个QR码,并在视图中显示它,或者直接在视图中创建它。
默认情况下,QR是以SVG格式生成的,你可以将其改为PNG,但必须安装imagick 扩展,否则会显示错误。
你可以从这里了解更多关于这个包的信息。