
根据 [mewebstudio/captcha](https://github.com/mewebstudio/captcha) 的文档,我将为您提供在本地运行该 Laravel Captcha 包的完整步骤:
### 1. 前置要求
确保您的系统满足以下要求:
- PHP >= 7.4
- Laravel 5/6/7/8/9/10/11
- GD 库扩展
- PHP FileInfo 扩展
- PHP Mbstring 扩展
对于 Windows 用户,需要在 php.ini 中启用以下扩展:
```ini
extension=php_gd2.dll
extension=php_fileinfo.dll
extension=php_mbstring.dll
```
### 2. 安装步骤
1. **创建新的 Laravel 项目**(如果还没有):
```bash
composer create-project laravel/laravel your-project-name
cd your-project-name
```
2. **通过 Composer 安装 captcha 包**:
```bash
composer require mews/captcha
```
3. **发布配置文件**:
```bash
php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"
```
### 3. 配置
1. **配置文件位置**:`config/captcha.php`
默认配置示例:
```php
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true, // 启用数学验证码
'expire' => 60, // 验证码过期时间(秒)
],
];
```
### 4. 使用示例
1. **创建路由**(在 `routes/web.php` 中):
```php
use Illuminate\Support\Facades\Route;
Route::get('/captcha-test', function () {
return view('captcha-test');
});
Route::post('/captcha-test', function () {
request()->validate([
'captcha' => 'required|captcha'
]);
return 'Captcha verified successfully!';
});
```
2. **创建视图**(创建文件 `resources/views/captcha-test.blade.php`):
```html
<!DOCTYPE html>
<html>
<head>
<title>Captcha Test</title>
</head>
<body>
<form method="POST" action="/captcha-test">
@csrf
<div>
{!! captcha_img() !!}
</div>
<div style="margin-top: 10px;">
<input type="text" name="captcha" placeholder="Enter Captcha">
</div>
@error('captcha')
<div style="color: red;">{{ $message }}</div>
@enderror
<div style="margin-top: 10px;">
<button type="submit">Verify Captcha</button>
</div>
</form>
</body>
</html>

4530

被折叠的 条评论
为什么被折叠?



