Check that there is a request in a route post

0

I have a form on the localhost page: 8000 / package that is sent to the url localhost: 8000 / book using the post method, everything works perfect, but if in the address bar I type localhost: 8000 / book and I give enter throws me the following error

MethodNotAllowedException
at RouteCollection->methodNotAllowed(array('POST'))in RouteCollection.php (line 238)

I think it's because when I sent the form the parameters for the route localhost: 8000 / book are sent by post, and when I want to enter the URL directly as there are no parameters that are sent, I mark that error.

How can I validate the route saying that if there is no $ request or parameters, I redirect to another url? This as a way to protect the url and not allow users to enter this.

    
asked by Jesus 23.06.2017 в 18:38
source

2 answers

0

In fact the exception of method not allowed is almost a way to protect the URL.

This happens because when you type the URL in the browser you are making a GET request, while with the form you make a POST request.

"The protection" against those users who might try to send information directly to the script through POST is given by the token that you should have included in some form in the form. See link

If you definitely want to redirect the URL when sending a request via GET, then you must add the respective path and simply pass a redirect to the controller that receives the request.

Just in case, there is ALWAYS a request every time you enter a "URL", regardless of the verb, maybe you mean check there is a data or a value?

I will give more details according to what the OP needs:

Route::get('book', 'controlador@metodo');

On the controller:

public function metodo(Request $request)
{
    return redirect()->route('home');
}
    
answered by 23.06.2017 / 18:52
source
0

Good Jesus, you can check if there is a variable in the request and decide what to do if it exists or not, but you would have problems since you do not know exactly if it comes by POST or by GET:

public function index(){
    if(Request::exists('foo')) //comprueba si existe un variable llamada foo en el request
        return redirect->route(ruta.la.vista);
    else
        return redirect->route(otra.ruta.vista); 
}

I recommend you differentiate the method if you are making a POST or GET request with:

if (Request::isMethod('get'))
{
    return redirect->route(index);
}
    
answered by 23.06.2017 в 18:52