通過摸索,終於能在laravel 5.1中應用驗證碼了。php
由於英語渣五水平,因此幾乎沒搜索到什麼有用的,因而考慮在github上搜索驗證碼包!css
提示: github上的package中每每會有使用說明,照着上面的實現,通常都能作出來。html
我使用的是mews captchahtml5
git 上的地址: https://github.com/mewebstudio/captcha 上面的使用很詳細了。jquery
動手實現:laravel
-- 手動進入 laravel 項目目錄git
-- 在對應目錄下,找到composer.json文件,打開它,添加以下語句:github
如圖:添加語句web
{ "require": { .... .... .... "mews/captcha": "~2.0" }, "minimum-stability": "dev"}
-- 執行composer update, 若是報錯,請先執行composer self-update, 而後再執行 composer updateajax
-- 找到config/app.php打開,添加以下語句:
1.打開這個文件,找到providers項,添加以下語句
Mews\Captcha\CaptchaServiceProvider::class,
2.找到aliases項,添加以下語句:
添加語句:
'Captcha' => Mews\Captcha\Facades\Captcha::class,
-- cmd窗口中執行 php artisan vendor:publish
經過上面的全部步驟,查看目錄,你會發如今vendor下多了mews目錄,而且你可使用命名空間 use Captcha, 在config/captcha.php中存在一個驗證碼的配置文件captcha.php。
-- 上面的全部步驟在 https://github.com/mewebstudio/captcha 中一樣存在!
咱們來看一看新增的vendor/mews目錄
咱們找到vendor/mews/captcha/src/captcha.php
查找public 方法, 發現:
public function create($var) --- 生成驗證碼
public function check($var) --- 判斷輸入和驗證碼是否相等
public function src($var) --- 輸出img屬性src的連接
public function img($var) --- 輸出img標籤
-- 其中,$var 參數就是,config/captcha.php 中 對應的鍵,分別能夠是:
default, flat, mini, inverse 表明了四種風格的驗證碼!能夠針對不一樣風格進行配置,直接在config/captcha.php中修改配置項
-- 實例:
1.在app/Http/routes.php中配置路由
其中captcha/test, 表示測試路徑, 映射 CaptchaController 下的 public function index() {}
captcha/mews 映射驗證碼的輸出,映射 CaptchaController 下的 public function mews() {}
2.建立控制器
在cmd窗口中,直接輸入
php artisan make:controller CaptchaController --plain
若是提示成功,則說明在 app/Http/Controllers/ 下產生了新文件 CaptchaController.php
3.在app/Http/Controllers/CaptchaController.php中新建index() 和 mews() 方法
<?phpnamespace App\Http\Controllers;use App\Http\Requests;use App\Http\Controllers\Controller;use Illuminate\Http\Request;//引用對應的命名空間use Session;use Captcha;class CaptchaController extends Controller { /** * 測試頁面 */ public function index() { // $mews = Captcha::src('inverse');, compact('mews') return view('captcha.index'); } /*建立驗證碼*/ public function mews() { return Captcha::create('default'); } }
4.建立視圖
在resources/views/目錄下建立captcha目錄,分別新建index.blade.php 而且大概作個佈局和樣式調整
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Learn Laravel 5.1</title> <link href="{{ asset('/css/app.css') }}" rel="stylesheet"> <!-- Fonts --> <link href='//fonts.useso.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--></head><body> <h2>該頁面僅僅用來學習驗證碼</h2> <h3>mews</h3> @if(count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ url('test/cpt') }}" method="post"> <input type="hidden" value="POST" name="_method"><!-- --><input type="hidden" value="{{ csrf_token() }}" name="_token" /><!-- --><input type="text" name="cpt" value="" /><!-- --><img src="{{ url('captcha/mews') }}" onclick="this.src='{{ url('captcha/mews') }}?r='+Math.random();" alt=""><!-- --><input type="submit" value="Submit" /> </form> <div id="footer" style="text-align: center; border-top: dashed 3px #eeeeee; margin: 50px 0; padding: 20px;"> ©2015 <a href="http://www.cnblogs.com/Zell-Dinch/">ZellDincht</a> </div> <!-- Scripts --> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script></body></html>
這時候輸入:http://your-host/captcha/test 查看
這是個人佈局和captcha
5. 添加路由到app/Http/routes.php
我經過post提交到後臺進行測試
6.在TestController中cpt方法以下:
<?phpnamespace App\Http\Controllers\Test;use Illuminate\Http\Request;use App\Http\Requests;use App\Http\Controllers\Controller;use Input, Validator, Redirect, Session, Captcha;class TestController extends Controller{ public function cpt(Request $request) { // dd(Input::get('cpt')); $rules = [ "cpt" => 'required|captcha' ]; $messages = [ 'cpt.required' => '請輸入驗證碼', 'cpt.captcha' => '驗證碼錯誤,請重試' ]; //若是僅僅驗證captcha的值能夠 //採用 Captcha::check(Input::get('cpt')); //返回bool $validator = Validator::make(Input::all(), $rules, $messages); if($validator->fails()) { return Redirect::back()->withErrors($validator); } else { return "驗證碼OK!"; } } }
以上,測試以下,
驗證錯誤:
驗證正確:
這裏沒有采用使用Captcha::check()的方式,具體狀況具體分析吧。