【问题标题】:Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219Laravel 5.2:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException
【发布时间】:2016-06-20 20:14:11
【问题描述】:

我想通过我的任务控制器保存一份表单数据。但是当我去 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

【问题讨论】:

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


    【解决方案1】:

    正如我所见,TestController@create 是一个 post 方法。但它的行为类似于 get 方法。尝试将 Request $request 参数传递给 create 方法。否则,如果您确实需要 create 方法的 get 方法,请更改像这样在Routes.php中获取方法,

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

    【讨论】:

      【解决方案2】:

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

      'method' => 'POST'
      

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

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

      【讨论】:

        【解决方案3】:

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

        试试

        'method' => 'patch'
        

        改成

        'method' => 'post'
        

        【讨论】:

        • 错字..我改了。还是一样的问题。
        • 尝试用 POST 方法为 Test.store 定义路由
        猜你喜欢
        • 2016-06-15
        • 2016-07-30
        • 2016-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        相关资源
        最近更新 更多