简体   繁体   中英

Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219

I want to save one form data through my task controller. But when i go to url to access my form. it's showing the following Error:

MethodNotAllowedHttpException in RouteCollection.php line 219:

Here is my 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');
});

Here is my 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');

    }   
}

Here is the create_item page( post form / view page)

@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

You're using PATCH method in form, but route with POST method

try

'method' => 'patch'

change to

'method' => 'post'

LaravelCollective's HTML only supports methods POST, GET, PUT DELETE so you might want to change that to POST or PUT

'method' => 'POST'

You haven't declared a Test.store route in your Routes.php , so try adding a resource or a named route:

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

正如我所看到的,TestController@create 是一个 post 方法。但它的行为就像一个 get 方法。尝试将 Request $request 参数传递给 create 方法。否则,如果你真的需要 create 方法的 get 方法,请将方法更改为 get在 Routes.php 像这样,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM