Laravel-5.1 ---- 將mews captcha整合到項目中!


經過摸索,終於能在laravel 5.1中應用驗證碼了。

因為英語渣五水平,所以幾乎沒搜索到什么有用的,於是考慮在github上搜索驗證碼包!

提示: github上的package中往往會有使用說明,照着上面的實現,一般都能做出來。

 

我使用的是mews captcha

git 上的地址:https://github.com/mewebstudio/captcha 上面的使用很詳細了。

 

動手實現:

-- 手動進入 laravel 項目目錄

 

-- 在對應目錄下,找到composer.json文件,打開它,添加如下語句:

如圖:添加語句

{
    "require": {
        ....
        ....
        ....
        "mews/captcha": "~2.0"
    },
    "minimum-stability": "dev"
}    

 

-- 執行composer update, 如果報錯,請先執行composer self-update, 然后再執行 composer update, 還是不行就 composer install

 

-- 找到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() 方法

<?php
namespace 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方法如下:

<?php

namespace 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()的方式,具體情況具體分析吧。

 

如果對你有幫助,請點下推薦啦 ^_^,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM