添加表单上的不匹配令牌laravel

时间:2015-07-24 08:03:34

标签: php mysql laravel

我有这个表格来创建一篇新文章。用户正确输入所有内容但我得到TokenMismatchException in VerifyCsrfToken.php line 53:的回报。我看到另一篇文章说,因为我使用了开放表格标签,它已经为我添加了这个并将其取出。我该如何解决这个问题?还是有其他一些我忽视的问题?

这是我的控制器

public function store( AddArticleController $request)
{
    $request= $request->all();
    $request['user_id']=Auth::id();
    Article::create($request->all());


    return redirect('/user');
}

这是html

<form class="form-horizontal" role="form" method="POST" action="{{ url('/article/add') }}">
                {!! Form::open(
                array(
                'class' => 'form',
                'novalidate' => 'novalidate',
                'files' => true)) !!}
                <div class = "form-group">
                    {!! Form::label('title','Title:', ['class' => 'col-md-4 control-label']) !!}
                    <div class="col-md-6">
                    {!! Form::text('title',null, ['class' => 'form-control']),  !!}

                    </div>
                </div>
                <div class = "form-group">
                    {!! Form::label('title','Image:', ['class' => 'col-md-4 control-label']) !!}
                    <div class="col-md-6">
                        {!! Form::file('image') !!}
                    </div>
                </div>
                <div class="form-group">
                    {!! Form::label('title','Type:', ['class' => 'col-md-4 control-label']) !!}
                    <div class="col-lg-1">
                    {!! Form::select('type', array('select' => 'select','fashion' => 'Fashion', 'music' => 'Music', 'dance' => 'Dance', 'event' => 'Event'))!!}
                    </div>

                </div>
                <div class = "form-group">
                    {!! Form::label('body','Comment:', ['class' => 'col-md-4 control-label']) !!}
                    <div class="col-md-6">
                        {!! Form::textarea('body',null, ['class' => 'form-control']) !!}
                    </div>
                </div>
                <div class = "form-group">
                    {!! Form::label('body',' ', ['class' => 'col-md-4 control-label']) !!}
                        <div class="col-md-6">
                    {!! Form:: submit('Sumbit', ['class' => 'btn btn-primary form-control']) !!}
                        </div>
                    </div>
                {!! Form::close() !!}
            </form>

2 个答案:

答案 0 :(得分:1)

您应该从代码中删除<form>代码或{{ Form::open() }}

Laravel的表单生成器(自Laravel 5以来已被删除)允许您编写呈现为普通html标记{{ Form::open() }}的语法<form>

所以在你的情况下,正确的方法是

<form class="form form-horizontal" novalidate="novalidate" enctype="multipart/form-data" role="form" method="POST" action="{{ url('/article/add') }}">
{!! csrf_token() !!}     // note that in this case we need to put csrf token manuall

......
</form>

OR

{!! Form::open(['action' => url('/article/add'), 'novalidate' => 'novalidate' 'files' => true, 'class' => 'form form-horizontal']) !!}
... //your fields and you don't have to explicitly include csrf token. Form::open() will do that for you
{!! Form::close() !!}

答案 1 :(得分:1)

首先,您使用哪个版本的Laravel?

Laravel的HTML Helper已在Laravel的第5版中删除,现在尚未更新。如果您仍想使用此帮助程序,我建议您使用社区https://github.com/LaravelCollective/html更新的版本。

对于您的问题,如果您不使用帮助程序,则需要在表单中添加{!! csrf_token() !!}来手动添加令牌。您可以在文档中找到更多信息:http://laravel.com/docs/5.1/routing#csrf-protection