繁体   English   中英

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

[英]Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219

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

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

这是我的任务控制器

<?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

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

尝试

'method' => 'patch'

改成

'method' => 'post'

LaravelCollective 的 HTML仅支持 POST、GET、PUT DELETE 方法,因此您可能希望将其更改为 POST 或 PUT

'method' => 'POST'

您还没有宣布Test.store在你的路线Routes.php ,所以尝试添加的资源或命名路线:

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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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