Laravel,在验证ReCaptcha之后将数据添加到数据库

时间:2019-03-11 00:02:48

标签: laravel recaptcha

我在Laravel项目中使用ReCaptcha,并以此完成 tutorial

我需要创建一个页面,用户可以在其中检查验证码后发布其消息。 我创建了一个模式对话框,用户可以在其中填写如下数据:

<form class="form-horizontal" action="" method="post">
              <div class="form-group error">
                <label for="messageName" class="col-sm-3 control-label">Name</label>
                <div class="col-sm-9">
                  <input type="text" class="form-control has-error" id="name" name="name" placeholder="Your name" value="" 
                  ng-model="message.name" ng-required="true">
                  <span class="help-inline" 
                  ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
                </div>
              </div>
              <div class="form-group error">
                <label for="messageEmail" class="col-sm-3 control-label">Email</label>
                <div class="col-sm-9">
                  <input type="email" class="form-control has-error" id="email" name="email" placeholder="E-mail" value="" 
                  ng-model="message.email" ng-required="true">
                  <span class="help-inline" 
                  ng-show="GBM.email.$invalid && GBM.email.$touched">Required</span>
                </div>
              </div>
              <div class="form-group error">
                <label for="messageLink" class="col-sm-3 control-label">Web</label>
                <div class="col-sm-9">
                  <input class="form-control" rows="3" class="form-control has-error" id="web" name="web" placeholder="Link for your web" value="" ng-model="message.web" ng-required="false" >
                </div>                    
              </div>

              <div class="form-group error">
                <label for="messageText" class="col-sm-3 control-label">Comment</label>
                <div class="col-sm-9">
                  <textarea class="form-control" rows="3" class="form-control has-error" id="comment" name="comment" placeholder="Your comment" value="" ng-model="message.text" ng-required="true" ></textarea> 
                  <span class="help-inline" 
                  ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
                </div>                    
              </div>

                {!! csrf_field() !!}
                <!-- recaptcha -->
                {{Request::is('contactd')}}
                <div class="form-group">

                    <div class="col-md-9">
                        <div class="g-recaptcha" data-sitekey="{{env('GOOGLE_RECAPTCHA_KEY')}}"></div>
                    </div>
                </div>
                <!-- Button -->
                <div class="form-group">
                    <label class="col-md-9 control-label"></label>
                    <div class="col-md-9">
                        <button type="submit" name="send" class="btn btn-primary btn-lg btn-block">Add new message <span class="fa fa-paper-plane-o"></span></button>
                    </div>
                </div>
            </form>

对于一条路线,我是这样的:Route::post('contact','ContactController@store');

这是问题所在,在我的控制器中,我得到了以下代码来验证验证码:

public function store(ReCaptchataTestFormRequest $request){
return "Captcha done right! ";}

此代码可将数据保存到数据库

    public function store(Request $request)
{
    $this->validate($request, [ 'name' => 'required|max:255' ]);
    $this->validate($request, [ 'email' => 'required | email' ]);
    $this->validate($request, [ 'comment' => 'required' ]);

    $ip = $_SERVER['REMOTE_ADDR'];
    $browser = $_SERVER['HTTP_USER_AGENT'];

    $guestbook = Guest_books::create([
        'name' => $request->input('name'),
        'email' => $request->input('email'),
        'web' => $request->input('web'),
        'comment' => $request->input('comment'),
        'ip' => $ip,
        'browser' => $browser
    ]);

    return $guestbook;
}

问题是:在Controller中为项目编写什么内容以验证Captcha,然后将其发布到数据库中?

0 个答案:

没有答案