本文介绍了Laravel错误:RouteCollection.php中的MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经为Excel上传创建了代码,我收到了以下提到的错误...

Having creating a code for Excel upload I am getting the below mentioned error...

用VIEW编写的代码是

The codes written in VIEW is

视图/项目/项目

  @extends('layouts.master')

@section('content')
      <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-6">
          <div class="row">
<form action="{{route('items.import')}}" method="POST" enctype="multipart/form-data">
              <div class="col-md-6">
                {{csrf_field()}}
                <input type="file" name="imported-file"/>
              </div>
              <div class="col-md-6">
                  <button class="btn btn-primary" type="submit">Import</button>
              </div>
            </form>
          </div>
        </div>
        <div class="col-md-2">
         <!-- <button class="btn btn-success">Export</button> -->
        </div>
      </div>

    @endsection

route.php中编写的代码是...

The codes written in route.php is...

Route::get('/items', 'ItemController@index');

Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController@import']);

ItemController.ASPX

ItemController.ASPX

 public function index()
    {
      return view('items.items');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
public function import(Request $request)
    {
      if($request->file('imported-file'))
      {
                $path = $request->file('imported-file')->getRealPath();
                $data = Excel::load($path, function($reader) {
            })->get();

            if(!empty($data) && $data->count())
      {
        $data = $data->toArray();
        for($i=0;$i<count($data);$i++)
        {
          $dataImported[] = $data[$i];
        }
            }
      Inventory::insert($dataImported);
        }
        return back();
  }

任何人都可以帮助我,在输出错误的代码中缺少什么...

Can anyone please help me what am missing in my coding that outputs the error...

推荐答案

尝试使用以下代码代替您的代码:

Try this code instead of yours:

Route::post('/items/import',[ 'as' => 'items/import', 'uses' => 'ItemController@import']);

诀窍是-路线需要命名.为了避免将来造成混乱,最好将其命名为"items.import",以便稍后可以自己确定这是一条路线的名称".所以最终的代码将是:

The trick is - the route needs to be named.To avoid future confusion, it's better to name it as "items.import", so later you can identify for yourself that this is a "name" of a route.So the final code would be:

Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController@import']);

在刀片模板中,您这样称呼它:

and in blade template u call it like that:

<form action="{{route('items.import')}}"...

这篇关于Laravel错误:RouteCollection.php中的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:30