Laravel 5.2 pages redirecting to POST without showing validation errors

3

In laravel 5 whenever I try to send a post to save or edit the data, a redirection occurs in all requests with status 302.

This is occurring on any system page where I make a POST to insert the data into the database.

I'm using% custom Requests to validate data, but no validation error is being displayed.

Can anyone tell me what can cause this in laravel 5?

follows my route code.

Route::group(['middleware' => ['web','auth']], function(){

   Route::any('usuario/salvar','UsuarioController@anySalvar');

});

In the tests I made the method is accessed, however it has redirection and does not display any errors. I'm using Laravel and the validation of Session Flash , which should return the errors in the view within the Laravel variable, but no error messages are displayed.

    
asked by anonymous 06.05.2016 / 16:26

1 answer

2

In%%, all routes must pass through the middleware group called Laravel 5.2 .

What happens is that in some releases of web , the Laravel 5.2 settings are as follows:

  /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace, 'middleware' => 'web'], function ($router) {
            require app_path('Http/routes.php');
        });
    }

Note that it has already been stated there that all routes you create within RouteServiceProvider will have the settings set in Http/routes.php . In your case, we have verified that it is so, so when you have made your $router->group statement, you are causing this ['middleware' => ['web','auth'] to be processed again.

Incredibly, the Middleware middleware group is responsible for session preprocessing.

When you talk about "no error is being displayed," you're probably talking about validation errors, which use web , which in turn use MessageBag .

The Session Flash is created only once, and when it is accessed, it is removed. If you call Flash twice because of redreaction, it is obvious that on the second call middleware will no longer exist.

So the solution to your problem is simply to remove% with% from your Session Flash , thus:

['middleware' => ['auth']

So we have been able to solve the problem.

It is worth mentioning that in some versions of web , this method does not come with the web middleware, being necessary to add either the Route Group declaration or the RouteServiceProvider declaration.

In my project for example, it looks like this:

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
    
06.05.2016 / 17:26