Laravel 5.2 Redirect::back()->with and Redirect::back()->withErrors does not work

Asked

Viewed 98 times

1

I have already developed several applications with Laravel. The problem arose when suddenly in version 5.2 the Redirect::back()->with and Redirect::back()->withErrors command stopped working. I searched on the internet and only found solution of type to add the web middleware in the Routes. Thing I do. See an example of my route that doesn’t work:

    Route::group(['middleware' => 'web'], function() {
        Route::get("/teste", function(){
            return \Illuminate\Support\Facades\Redirect::back()->with('success', ['Database Error!']);
        });

        Route::get("/teste2", function(){
            if(Session::has("success")) echo Session::get('success');
        });
    });

It is not working at all, before my Lade is as follows and also did not work.

            <div class="row">
            <div class="large-12 medium-12 small-12 columns ErroPanel">
                @if ($errors->any())
                    <div class="alert callout closable">
                        <ul class="alert alert-warning">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif
                @if (Session::has("success"))
                    <div class="success callout closable">
                        <ul class="alert alert-success">
                            @foreach(\Illuminate\Support\Facades\Session::get("success") as $success)
                                <li>{{ $success }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif
            </div>
        </div>

Kernel.php

    <?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

Solution

I did the removal of the line Route::group(['middleware' => 'web'], function() { from my route and started working, which I do not understand that my file came with this line as default, but already Via been added as default also in Routeserviceprovider giving conflict. Finally, now it’s working, I must open an Issue in the git of the Laravel stating the error?

  • with does not have to take as parameter an array?

  • No, he is the same way. with('name', 'value').

  • How’s your Http Kernel @Pedrosoares ?

  • I just added it.

  • Surely, this question, even if it is not duplicated, already has an answer.

  • I don’t know @Wallacemaxters, he is applying the middleware directly on the route... it was to work independent of Routeprovider....

  • @gmsantos yes, but if you observe, depending on the version, as I said in the reply, the middleware will be declared twice, causing problems in the session. At least that’s what happened to me, but yes, it could be another case.

Show 2 more comments
No answers

Browser other questions tagged

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