0
votes

I'm new in Laravel!!

When using return redirect::back() from my Controller to a POST view I get a error in Laravel 4.2.

Error: Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

My Route:

Route::post("productos_categoria", array(
"as"=>"productosCategoria",
"uses"=>"ProductosController@mostrarPorCategoria"
));

My function controller:

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();
}

Redirect::back() is only for GET views or GET Routes??

How return a previous view from my controller using the post method??

1
Try using return redirect()->back(); - Narendrasingh Sisodia
I try but fails Error: Call to undefined function redirect() - J-David Alpízar
That depends on where the request came from. if your current url where form placed should use GET method.then you can redirect back to GET url from the post request. otherwise error will be thrown. - adarshaU

1 Answers

1
votes

Generally you cannot redirect to a POST method reason being

Laravel's Redirect uses the HTTP response redirect method. There is no redirect method with POST in HTTP hence it doesn't exist in Laravel.

But with some search on different places i found a solution to follow

The steps are:

  1. Extend the Redirector class to record additional info on the intended request.

  2. Create a new Service Provider that writes over "redirect" in the IOC container.

  3. Create a new service provider which will set the intended method and input after the redirect.

  4. Finally add your new service providers to your providers array in app/config/app.php

For detailed code and idea on this follow below link on Laravel.io

http://laravel.io/forum/04-23-2014-redirectintended-with-routepost