【问题标题】:Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219RouteCollection.php 第 219 行中的 Laravel 5 MethodNotAllowedHttpException
【发布时间】:2016-08-30 10:02:13
【问题描述】:

我正在尝试在 Laravel 5 中制作图像上传器,但我仍然收到此错误:

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

什么会导致这个问题?

表格:

<form name="upload_image" method="post" action="{{URL::route('uploadImage')}}">
<input type="file" accept="image/*">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="submit">

路由.php

Route::post('uploadImage', [
    'as' => 'uploadImage',
    'uses' => 'HomeController@uploadImage'
]);

HomeController.php

public function uploadImage() {
    if (Auth::check()) {
        if (Auth::user()->admin == 1) {
            $image = Input::get('image');
            $filename  = time() . '.' . $image->getClientOriginalExtension();
            $path = public_path('articleImages/' . $filename);
            Image::make($image->getRealPath())->resize(600, 400)->save($path);
            return view('admin.uploadImage')->with('path', $path);
        }
        return view('/');
    }
    return view('/');
}

谢谢。

【问题讨论】:

  • 旁注,在 Laravel 5 中只使用 route('uploadImage') 而不是 URL::route('uploadImage')。由于 URL 工厂已被弃用。
  • 您从哪里听说UrlGenerator 已被弃用? URL 只是一个门面,不要认为它会被弃用......
  • 我不是导致此错误的原因,我更改了它,但还是一样。
  • 在这个之前的routes.php文件中是否声明了任何路由?
  • 整个文件:pastebin.com/chg4p66b

标签: php laravel exception laravel-5 routing


【解决方案1】:
  1. 在 Laravel 5 中,您必须使用 {{ route('uploadImage') }} 而不是 {{URL::route('uploadImage')}},因为 Laravel 不再使用 URL 提供程序。

  2. 您是否忘记在表单中输入enctype="multipart/form-data"

【讨论】:

    【解决方案2】:

    首先你需要输入元素的名字 像这样:

    <form name="upload_image" method="post" action="{{URL::route('uploadImage')}}">
    <input name="image" type="file" accept="image/*">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="submit" name="submit">
    

    第二件事,你可以这样写路线:

    Route::post('uploadImage','HomeController@uploadImage');
    

    【讨论】:

      【解决方案3】:

      更改 URL::路由

      <form name="upload_image" method="post" action="{{route('uploadImage')}}">
      

      【讨论】:

      • 我不是导致此错误的原因,我更改了它,但还是一样。
      • 尝试将文本改为uploadimage而不是uploadImage
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2016-06-20
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多