保护模型绑定的Laravel表单


Securing Model-Bound Laravel Forms

我有一个使用模型绑定将数据输入数据库的表单。我试图通过https提供服务,但无法弄清楚。

视图如下:

    {!! Form::model(new App'MissingHours, ['route' => ['missinghours.store'], 'class' => 'form-horizontal']) !!}
        @include('missinghours/_form', ['submit_text' => 'Submit Hours'])
    {!! Form::close() !!}

我试过将url设置为https://appurl/missinghours/store,但显然不起作用。我还尝试了model_secure在Form::open_secure之后,这不起作用。当我通过https提供页面并尝试提交表单时,我得到一个关于它不安全的警告,并且没有提交数据。

控制器:

 $input = Input::all();
 $save = MissingHours::create( $input );

如果您需要您的整个网站是https -包括表单,您可以在public/.htaccess文件RewriteEngine On下面插入以下代码:

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
编辑:

你可以像这样创建一个before中间件:

<?php
namespace App'Http'Middleware;
use Closure;
class BeforeMiddleware
{
    public function handle($request, Closure $next)
    {
        if (! $request->secure()) return redirect()->secure($request->getRequestUri());
        return $next($request);
    }
}

然后在表单中使用该中间件。