尽管添加了CSRF令牌,但发布方法无效

时间:2018-04-25 09:22:22

标签: laravel

我是Laravel的新手,我尝试使用表单,因此在添加CSRF令牌后使用了post方法。我也下载了form builder package并进行了所需的所有调整。但不知何故,post中的routes方法无效。虽然我尝试在路线中使用getany方法,但它们完美无缺。

Controller

use App\Http\Controllers\Controller;

class AdminController extends Controller
{
    public function index()
    {
        echo "admin controller";
    }

    public function formSubmit()
    {
        echo "Form Submit Method";
        return view('layouts\formView');
    }
}

View

@extends('layouts\masterLayout')
@section('head')

{!!Form::open(['route'=>'f.submit'])!!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!!Form::text('field-one')!!}
{!!Form::submit('')!!}
@endsection

Route

Route::post('form-submit',['uses'=>'Admin\AdminController@formSubmit', 'as'=>'f.submit']);

错误:堆栈跟踪

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

2 个答案:

答案 0 :(得分:1)

更改

{!!Form::open(['route'=>'f.submit'])!!}

{!!Form::open(['route'=>'f.submit', 'method' => 'POST'])!!}

原因可能很明显; - )

修改

您可以将formSubmit功能的签名更改为:

public function formSubmit(Request $request)
{
    echo "Form Submit Method";
    return view('layouts\formView');
}

答案 1 :(得分:0)

而不是

使用以下方法创建具有唯一标记

的输入字段

{{csrf_token()}}

将路线更改为

路线::帖子(&#39;表格提交&#39;,&#39; as&#39; =&gt;&#39; f.submit&#39;,&#39;使用&#39; =&gt;&#39; Admin \ AdminController @ formSubmit&#39;]);

更改查看方法

return view('layouts\formView');

return view('layouts.formView');

对于E.g

return view('YourFolderName.FileName');