通过 laravel 中间件将数据从会话传输到视图
Transfer data from session to views through laravel middleware
我正在检索会话中已添加到购物车的商品,并计算商品总数。我创建了一个数组,其中添加了这些项目,我用它来显示购物车。我获取购物车中的商品数量并在每个视图中显示该数量。我已经制定了代码,但到目前为止,使其工作的唯一方法是将其复制并粘贴到每条路线中。研究表明使用中间件是在所有视图之间共享变量的方法,但我一直无法让它工作。
首先,这是中间件的代码。
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
use App\Models\Item;
class GetCart
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$items = Item::all();
$cartItems = [];
for($i=0; $i < count($items); $i++){
if(null !== Session::get('cartItem'.$i)){
$item = Session::get('cartItem'.$i);
array_push($cartItems, $item);
}
};
$totalNum = 0;
for($i=0; $i < count($cartItems); $i++){
if(isset($cartItems[$i])){
$totalNum += $cartItems[$i]['quantity'];
}
}
View::share('cartItems', $cartItems);
View::share('totalNum', $totalNum);
return $response;
}
}
在 App\Http\Kernel.php 中,我将它添加到 $middleware 变量中,这使得它在每次请求时自动加载。
protected $middleware = [
\App\Http\Middleware\GetCart::class
];
但是,我仍然无法检索变量。它说 'undefined variable.' 因此,对于我遗漏的内容以及为什么我无法在所有视图中共享此变量,我将不胜感激。正如我所说,我可以通过将代码复制并粘贴到每条路线中来使其工作,但我知道这不是正确的方法。
这里的问题是您已经在中间件中处理响应。参见 https://laravel.com/docs/8.x/middleware#middleware-and-responses
在深入应用程序之前,您需要它。然后应该在所有视图中可用。
但我认为最好为购物车 (cart.blade.php) 制作一个单独的 blade 并将其包含在所有其他需要的视图中。然后为该购物车视图创建一个新的视图编辑器 class 并在其中共享您的 cartItems
和 totalNum
。
参见 https://laravel.com/docs/8.x/views#view-composers
我正在检索会话中已添加到购物车的商品,并计算商品总数。我创建了一个数组,其中添加了这些项目,我用它来显示购物车。我获取购物车中的商品数量并在每个视图中显示该数量。我已经制定了代码,但到目前为止,使其工作的唯一方法是将其复制并粘贴到每条路线中。研究表明使用中间件是在所有视图之间共享变量的方法,但我一直无法让它工作。
首先,这是中间件的代码。
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
use App\Models\Item;
class GetCart
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$items = Item::all();
$cartItems = [];
for($i=0; $i < count($items); $i++){
if(null !== Session::get('cartItem'.$i)){
$item = Session::get('cartItem'.$i);
array_push($cartItems, $item);
}
};
$totalNum = 0;
for($i=0; $i < count($cartItems); $i++){
if(isset($cartItems[$i])){
$totalNum += $cartItems[$i]['quantity'];
}
}
View::share('cartItems', $cartItems);
View::share('totalNum', $totalNum);
return $response;
}
}
在 App\Http\Kernel.php 中,我将它添加到 $middleware 变量中,这使得它在每次请求时自动加载。
protected $middleware = [
\App\Http\Middleware\GetCart::class
];
但是,我仍然无法检索变量。它说 'undefined variable.' 因此,对于我遗漏的内容以及为什么我无法在所有视图中共享此变量,我将不胜感激。正如我所说,我可以通过将代码复制并粘贴到每条路线中来使其工作,但我知道这不是正确的方法。
这里的问题是您已经在中间件中处理响应。参见 https://laravel.com/docs/8.x/middleware#middleware-and-responses 在深入应用程序之前,您需要它。然后应该在所有视图中可用。
但我认为最好为购物车 (cart.blade.php) 制作一个单独的 blade 并将其包含在所有其他需要的视图中。然后为该购物车视图创建一个新的视图编辑器 class 并在其中共享您的 cartItems
和 totalNum
。
参见 https://laravel.com/docs/8.x/views#view-composers