我试图在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">
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('/');
}
谢谢。
更改URL::路由
<form name="upload_image" method="post" action="{{route('uploadImage')}}">
首先需要输入元素的名称像这样:
<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');
-
对于Laravel 5,您必须使用
{{ route('uploadImage') }}
而不是{{URL::route('uploadImage')}}
,因为Laravel不再使用URL提供程序。 -
你忘了把
enctype="multipart/form-data"
放在表格里了吗?