Laravel如何在中间件内部使用一个变量函数



我正在使用Laravel和在控制器我使用一个中间件函数内部构造函数如下

class BaseController extends Controller
{
//
public function __construct(){        
$this->middleware(function ($request, $next) {
$selected_country_id = session()->get('browse_country_id')??4;

return $next($request);
});
// end of middleware function
//back to constructor function 
// How i can use selected_country_id here  for example 
dd($selected_country_id);
}
}

我的问题是如何使用变量$selected_country_id在中间件功能之外

将其Flash到请求中
$request->selected_country_id = $selected_country_id;
// or
$request->merge(["selected_country_id" => $selected_country_id]);
// or
$request->attributes->add(['selected_country_id' => $selected_country_id]);

和检索值

$request->selected_country_id;

最新更新