Laravel 5.2:RouteCollection.php第219行中的MethodNotAllowedHttpException

时间:2016-03-07 10:15:20

标签: php laravel laravel-5 laravel-routing laravel-5.2

我想通过我的任务控制器保存一个表单数据。但是当我去网址访问我的表格时。它显示以下错误:

  

RouteCollection.php第219行中的MethodNotAllowedHttpException:

这是我的Routes.php

<?php
    Route::group(['middleware' => 'web'], function () {
    Route::auth();

  Route::get('/', function () {
    return view('welcome');
    });

    Route::get('/all_item','TestController@index');
    Route::post('/create_item','TestController@create');
    Route::get('/home', 'HomeController@index');
});

这是我的TaskController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Test;
use App\Http\Requests;
use Redirect;

class TestController extends Controller
{
   public function index()
    {
            $alldata=Test::all();
    //      return $alldata;
            return  view('test.itemlist',compact('alldata'));
    }


    public function create()
    {
            return view('test.create_item');
    }


    public function store(Request $request)
    {       
            $input = $request->all();
            Test::create($input);       
            return redirect('test');

    }   
}

这是create_item页面(发布表单/视图页面)

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Create Item</div>
                {!! Form::open(array('route' => 'Test.store','class'=>'form-horizontal','method' => 'patch'))  !!}
                {!! Form::token(); !!}
                  <?php echo csrf_field(); ?>
        <div class="form-group">
          <label>Item Code</label>
          <input type="text" name="item_code" class="form-control"  placeholder="Code">
        </div>
        <div class="form-group">
          <label>Item Name</label>
          <input type="text" name="item_name" class="form-control"  placeholder="Name">
        </div>        
        <button type="submit" class="btn btn-default">Submit</button>
               {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection

3 个答案:

答案 0 :(得分:1)

您在表单中使用PATCH方法,但使用POST方法

进行路由

'method' => 'patch'

更改为

'method' => 'post'

答案 1 :(得分:1)

LaravelCollective的HTML only supports methods POST, GET, PUT DELETE 所以你可能想把它改成POST或PUT

'method' => 'POST'

您尚未在Test.store中声明Routes.php路线,因此请尝试添加资源或命名路线:

Route::post('/store_item', [
    'as' => 'Test.store', 'uses' => 'TestController@store'
]);

答案 2 :(得分:0)

我可以看到TestController @create是一个post方法。但它的行为类似于get方法。尝试将Request $ request参数传递给create方法。否则如果你真的需要一个get方法来创建方法,请更改像这样在Routes.php中获取的方法,

Route::get('/create_item','TestController@create');