jeudi 8 décembre 2016

How to use custom middleware with Laravel 404 exception

In my Laravel 5.2 app I have custom middleware called 'cart' that I use to keep track of the users cart contents across different routes.

It looks like this:

class CartMiddleware
{

    public function handle($request, Closure $next)
    {
        $cart = new Cart();
        $cart_total = $cart->total();

        view()->composer(['layouts.main'], function ($view) use ($cart_total) {
        $view->with('cart_total', $cart_total);
    });
    return $next($request);
}

}

Route::group(['middleware' => ['cart']], function () {
    Route::get('cart', 'CartController@show');
});

When ever my app raises a 404 exception the 404.blade.php view cannot render because it is missing the $cart_total that is supplied by the 'cart' middleware.

Is there a way to assign this 'cart' middleware to my exception?

  if ($e instanceof HttpException) {
        if ($request->ajax()) {
            return response()->json(['error' => 'Not Found'], 404);
        }
        return response()->view('errors.404', [], 404);
    }
    return parent::render($request, $e);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire