0

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);
zeros-and-ones
  • 4,227
  • 6
  • 35
  • 54
  • Is there a reason you're not storing this value in the session? – Joe Dec 08 '16 at 11:02
  • Instead of `layouts.main` you can try to use `partials.cart-stats` like `view()->composer('partials.cart-stats', function($view) use ($cart_total){$view->with('cart_total', $cart_total);});` and then `@include('partials.cart-stats')` maybe in a hidden `div` in both `layouts.main` as well as all your custom error pages for ex `404.blade.php`. However better way would be store your cart values in session. – Donkarnash Dec 08 '16 at 11:17
  • Placing the $cart_total into a view partial is a good idea, but I don't see how that fixes the issue. The cart is in the session, the middleware makes it available to the layout as the cart->class parses the cart data. I will look into creating a simple cart_total session variable that doesn't require parsing. Thanks for insight. – zeros-and-ones Dec 08 '16 at 16:37
  • I tested the code by placing the $cart_total in a view partial and then include that partial in all custom error pages as well - in a hidden div. The error pages are displayed without any hitch whenever there is a corresponding exception. – Donkarnash Dec 09 '16 at 05:49
  • Thanks for your reply. I ended up removing the CartMiddleware entirely, and handling the session updates when the cart is changed. This was my fix. – zeros-and-ones Dec 09 '16 at 23:04

1 Answers1

0

At Laravel 5.4 and probably some older ones you can modify the file app/exceptions/Handler.php and the function render like this:

if( is_a( $exception, \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class ) ) {
    return redirect()->route( 'error_404' );
}

// ... old code in the function ...

in this way every 404 errors are redirected to certain real route that acts like other routes of site.

You may also submit any data from current request to show a reasonable error at the target.

AMIB
  • 3,262
  • 3
  • 20
  • 20