404 页面未获取数据 laravel

404 page doesn't get data laravel

我遇到 404 页面中的数据问题。

Handler.php

   public function render($request, Exception $e)
    {
        if ($this->isHttpException($e)) {
            $statusCode = $e->getStatusCode();
            switch ($statusCode) {
                case '404':
                    $obj = new \App\Http\Controllers\SiteController(new \App\Repositories\CatalogsRepository(new \App\Catalog));
                    $obj->template = env('THEME') . '.404';
                    return response($obj->renderOutput());
            }
        }
        return parent::render($request, $e);
    }

SiteController 中的渲染输出

public function RenderOutput()
    {
        $catalogItems = $this->getCatalogs();
        $cart_info = $this->CartInfo();
        $menu = view(env('THEME') . '.menu')->with(['catalogs' => $catalogItems, 'cart_info' => $cart_info,'title'=>$this->title])->render();
        $this->vars = array_add($this->vars, 'cart_info', $cart_info);
        $this->vars = array_add($this->vars, 'menu', $menu);

        return view($this->template)->with($this->vars);
    }

当我登录时,在常规页面中我有注销、我的订单、个人信息等链接。如果购物车有商品,它会显示商品的数量和数量,但是当 404 出现时,就像我没有登录(有注册和登录链接)并且购物车中有 0 件商品和 0 件商品。为什么会这样?

那是 因为 Laravel 启动 session 如果 路线 匹配:

在您的 app/Http/Kernel.php 中尝试将此添加到您的 $middleware :

\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class