Laravel 5.2 pages redirecting in the POST without showing validation errors

Asked

Viewed 649 times

3

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

This is occurring on any page of the system where I make a POST to enter the data in the database.

I am using the Requests customized of Laravel in order to validate the data, but no validation error is being displayed.

Can anyone tell me what could cause this in Laravel 5?

follows the code of my route.

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

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

});

In the tests I did the method is accessed, but has the redirect and shows no errors. I am using Session Flash and the validation of Laravel, that should return the errors in the view inside the variable $errors, but no error message is displayed.

  • It sends the error there. By number I do not know what it is. It has to do with the CSFR Token ?

  • 302 is "temporarily moved" not knowable enough to answer the question they use this code to answer a request.

  • Put the contents of your controller and Routes, it may also be that when accessing usuario/salvar he directs to usuario/salvar/ and so you see this message, I recommend you read this: http://answall.com/help/mcve

  • 1

    @Guilhermenascimento did not put the details of the error. I work with him, and slapped the question, to better describe the problem

  • 1

    @Wallacemaxters takes advantage that you are close to him and slap him on the head to read the MCVE ... Fun :p --- Nice to know that you work together.

  • kkkkkkkkkkkkkkkkk

Show 1 more comment

1 answer

2


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

What happens is that in some releases of Laravel 5.2, the settings of RouteServiceProvider is 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 there is already declared that all the routes you create within Http/routes.php shall have the settings set out in $router->group. In your case, we verify that you’re like this, so when you made your statement ['middleware' => ['web','auth'], you’re making this one middleware be sued again.

Incredible as it seems, the Middleware group web is responsible for pre-processing the session.

When you talk about "no error is being displayed", you are probably talking about the validation errors, which use the MessageBag, who in turn use the Session Flash.

The Flash is created only once, and when it is accessed, it is removed. If you call the middleware twice because of the redeclaration, it is obvious that in the second call the Session Flash will cease to exist.

So the solution to your problem is simply to remove web of your array, being like this:

['middleware' => ['auth']

With that we can solve the problem.

It is worth noting that in some versions of Laravel 5.2, this method does not come with the web middleware, it is necessary to add either in the Route Group declaration or in the Routeserviceprovider declaration.

In my project for example, it’s 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');
        });
    }
  • In which specific release this change occurred?

  • @gmsantos honestly, I don’t know. But I downloaded the Laravel 5.2 at home and again here at the company. I noticed that even though the two were 5.2, there was a difference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.