简体   繁体   中英

Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219

I am trying to make image uploader in Laravel 5, but I am strill getting this error:

MethodNotAllowedHttpException in RouteCollection.php line 219

What can cause this problem?

Form:

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

routes.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('/');
}

Thank you.

更改 URL::route

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

first of all you need names for input elements like this :

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

2nd thing that you can write the routes like this :

Route::post('uploadImage','HomeController@uploadImage');
  1. With Laravel 5 you have to use {{ route('uploadImage') }} instead of {{URL::route('uploadImage')}} because Laravel no longer uses URL provider.

  2. Have you forgot to put enctype="multipart/form-data" in your form?

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