Problem with POST / GET routes

0

I was doing tests with routes in Larevel 5.1 , in local, and I found a possible design error of the Framework?

If I create a route like this (having the indicated method in the corresponding controller):

Route::post(
    'test/rooting',         
    'TestController@rooting'
);

I have to add the following:

Route::get(
    'test/rooting',         
    function () {

        return view( 'errors/404' );
});

So there are no problems, if the GET request is made, page 404 is loaded, but if this GET route is not added, what happens is that the typical message appears:

  

Whoops, looks like something went wrong.

This also happens if something simpler is done like:

Route::post( 
    'test/rooting2', 
    function () {

        return "bar";
});

Not only if the request is addressed to a controller ...

And the funny thing is that no error log is generated. I believe that the default behavior should be redirecting automatically to 404. I was confused for a few minutes until I saw it was a POST-type route ...

I do not know if this is documented somewhere, maybe in previous versions, or is, as I mentioned, some kind of error. Has anyone observed this? Is there any intentional reason behind? Was it changed in later versions?

Edited: I add the POST routes in the VerifyCsrfToken class in the $ except property, to be able to test without CSRF errors. I do the tests with Chrome / Postman. With debug to true the GET request shows the error:

  

Whoops, looks like something went wrong.

> 1/1 MethodNotAllowedHttpException in RouteCollection.php line 218: in
> RouteCollection.php line 218 at
> RouteCollection->methodNotAllowed(array('POST')) in
> RouteCollection.php line 205 at
> RouteCollection->getRouteForMethods(object(Request), array('POST')) in
> RouteCollection.php line 158 at
> RouteCollection->match(object(Request)) in Router.php line 750 at
> Router->findRoute(object(Request)) in Router.php line 659 at
> Router->dispatchToRoute(object(Request)) in Router.php line 635 at
> Router->dispatch(object(Request)) in Kernel.php line 236 at
> Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) at
> call_user_func(object(Closure), object(Request)) in Pipeline.php line
> 139 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
> Debugbar.php line 51 at Debugbar->handle(object(Request),
> object(Closure)) at call_user_func_array(array(object(Debugbar),
> 'handle'), array(object(Request), object(Closure))) in Pipeline.php
> line 124 at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
> in VerifyCsrfToken.php line 50 at
> VerifyCsrfToken->handle(object(Request), object(Closure)) at
> call_user_func_array(array(object(VerifyCsrfToken), 'handle'),
> array(object(Request), object(Closure))) in Pipeline.php line 124 at
> Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
> ShareErrorsFromSession.php line 49 at
> ShareErrorsFromSession->handle(object(Request), object(Closure)) at
> call_user_func_array(array(object(ShareErrorsFromSession), 'handle'),
> array(object(Request), object(Closure))) in Pipeline.php line 124 at
> Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
> StartSession.php line 62 at StartSession->handle(object(Request),
> object(Closure)) at call_user_func_array(array(object(StartSession),
> 'handle'), array(object(Request), object(Closure))) in Pipeline.php
> line 124 at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
> in AddQueuedCookiesToResponse.php line 37 at
> AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
> at call_user_func_array(array(object(AddQueuedCookiesToResponse),
> 'handle'), array(object(Request), object(Closure))) in Pipeline.php
> line 124 at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
> in EncryptCookies.php line 59 at
> EncryptCookies->handle(object(Request), object(Closure)) at
> call_user_func_array(array(object(EncryptCookies), 'handle'),
> array(object(Request), object(Closure))) in Pipeline.php line 124 at
> Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in
> CheckForMaintenanceMode.php line 44 at
> CheckForMaintenanceMode->handle(object(Request), object(Closure)) at
> call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'),
> array(object(Request), object(Closure))) in Pipeline.php line 124 at
> Pipeline->Illuminate\Pipeline\{closure}(object(Request)) at
> call_user_func(object(Closure), object(Request)) in Pipeline.php line
> 102 at Pipeline->then(object(Closure)) in Kernel.php line 122 at
> Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line
> 87 at Kernel->handle(object(Request)) in index.php line 53
    
asked by Orici 08.11.2017 в 11:15
source

2 answers

1

To handle the exception MethodNotAllowedHttpException you must first create a file in resources/views/errors/404.blade.php .

Then you must modify the method render of the file app / Exceptions / Handler.php

// Agregar esto a la cabecera
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

// Manejar la excepción
public function render($request, Exception $e)
{
    if ($e instanceof MethodNotAllowedHttpException) {
        return abort('404');
    }

    return parent::render($request, $e);
}

I hope it serves you.

    
answered by 08.11.2017 в 14:46
0

I'm sorry to disappoint you, but it's a mistake of yours in many places.

First, it does not send you a message because the config/app > debug you have at false . And second, it's a route type POST , so you can not access with a simple url, you have to create a form and add the csrf_token.

<form action="/test/rooting2" method="POST">
{{ csrf_field() }}
    <button>ENVIAR</button>
</form>
    
answered by 08.11.2017 в 13:10