Pkurg.SiteCaptcha
基於 mews/captcha
細節
生成配置檔案
% php artisan vendor:publish --provider='Mews\Captcha\CaptchaServiceProvider'
Copied File [/plugins/pkurg/sitecaptcha/vendor/mews/captcha/config/captcha.php] To [/config/captcha.php]
Publishing complete.
這裡測試 用該命令能否直接將外掛包裡的配置釋出出去,我的客戶沒許可權修改這個配置
截圖
效果
頁面元件
用法說明
安裝
外掛市場安裝
備用地址下載壓縮包
php artisan plugin:refresh pkurg.sitecaptcha
前臺頁面使用
使用SiteCaptchaComponent元件,可設定驗證碼型別,是否顯示重新整理按鈕,按鈕的class
元件預設的是
<img src="{{ sitecaptcha.getImg() }}" alt="captcha" class="captcha-img" data-refresh-config="{{ sitecaptcha.property('type') }}">
{% if sitecaptcha.property('showrefresh') == 'show' %}
<a class="refresh-captcha" id="refreshcaptcha"><span class="{{ sitecaptcha.property('iconclass') }}"></span></a>
{% endif %}
這裡修改為
<img id="refreshcaptcha" src="{{ sitecaptcha.getImg() }}" alt="captcha" class="captcha-img img-yzm pull-right" data-refresh-config="{{ sitecaptcha.property('type') }}">
js中是根據captcha-img類的data-refresh-config獲取驗證碼型別的,根據id=”refreshcaptcha”獲取重新整理按鈕的。修改後點選驗證碼重新整理
後臺驗證
<?php namespace RainLab\User\Components;
.
use Captcha;
.
/**
* Sign in the user
*/
public function onSignin()
{
$data = post();
.
//驗證碼驗證
if (!Captcha::check($data['captcha'])){
throw new ApplicationException('驗證碼錯誤');
}
.
}
也可以
<?php namespace RainLab\User\Components;
.
use Session;
.
/**
* Sign in the user
*/
public function onSignin()
{
$data = post();
$rules = [];
$rules['captcha'] = 'required|captcha_api:'. Session::get('captcha.key');
.
$validation = Validator::make($data, $rules);
if ($validation->fails()) {
throw new ValidationException($validation);
}
.
}
知識點
記錄用的的知識點或技巧,方便自己查詢,參考,複製
在外掛中註冊providers 和 aliases
<?php namespace Pkurg\SiteCaptcha;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function boot()
{
\App::register('\Mews\Captcha\CaptchaServiceProvider');
$alias = \Illuminate\Foundation\AliasLoader::getInstance()->alias('Captcha', 'Mews\Captcha\Facades\Captcha');
}
}
在路由中使用
<?php
Route::get('/get_captcha/{config?}', function (\Mews\Captcha\Captcha $captcha, $config = 'default') {
return $captcha->src($config);
});
本作品採用《CC 協議》,轉載必須註明作者和本文連結