1
votes

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.

3
Sidenote, in Laravel 5 just use route('uploadImage') not URL::route('uploadImage'). As the URL factory is deprecated.Ohgodwhy
Where did you heard that UrlGenerator was deprecated? URL is just a facade for that, don't think it will be deprecated...PeterPan666
I isn`t what is causing this error, i changed it, but still getting the same.xtrontross
Is there any route declared in the routes.php file before this one?PeterPan666
the whole file: pastebin.com/chg4p66bxtrontross

3 Answers

1
votes

Change URL::route

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

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');
0
votes
  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?