我是Laravel的新手!!
当使用return redirect::back()从我的控制器到POST视图时,我在Laravel 4.2中得到了一个错误。
错误:Symfony''Component''HttpKernel''Exception''MethodNotAllowedHttpException
我的路线:
Route::post("productos_categoria", array(
"as"=>"productosCategoria",
"uses"=>"ProductosController@mostrarPorCategoria"
));
我的功能控制器:
public function sumarVoto() {
$correoUsuario = Session::get('correoUsuario');
$id_producto = Input::get('idProducto');
$existeVoto = DB::table('voto_usuarios')
->where('CORREO', '=', $correoUsuario)
->where('ID_PRODUCTO', '=', $id_producto)
->count();
if($existeVoto > 0) {
Session::flash($id_producto, '¡Usted ya realizó el voto con anterioridad para este producto!');
return Redirect::back();
}
DB::table('voto_usuarios')->insert(array('CORREO' => $correoUsuario, 'ID_PRODUCTO' => $id_producto));
DB::table('productos')->where('productos.ID_PRODUCTO', '=', $id_producto)->increment('VOTO');
Session::flash($id_producto, '¡Voto ingresado con éxito!');
return Redirect::back();
}
重定向::back()仅用于GET视图或GET路由??
如何使用post方法从我的控制器返回以前的视图??
通常情况下,您无法重定向到POST
方法,原因是
Laravel的Redirect使用HTTP响应重定向方法。HTTP中没有POST重定向方法,因此Laravel中不存在POST重定向方法。
但在不同的地方进行了一些搜索,我找到了遵循的解决方案
步骤是:
-
扩展Redirector类以记录有关预期请求的其他信息。
-
创建一个新的服务提供商,在IOC容器中重写"重定向"。
-
创建一个新的服务提供者,该提供者将在重定向后设置预期的方法和输入。
-
最后,在app/config/app.php 中将您的新服务提供商添加到您的提供商阵列中
有关详细的代码和想法,请访问Laravel.io 上的以下链接
http://laravel.io/forum/04-23-2014-redirectintended-with-routepost