laravel - 在 laravel 的构造函数中使用会话值
问题描述
我坚持在 __constructor 中使用会话值,但它显示空值。我还在 kernel.php 中添加了一些配置,但仍然无法正常工作。请帮忙。
public function __construct()
{
/** PayPal api context **/
$this->middleware(function ($request, $next) {
$paypal_conf = \Config::get('paypal');
$orgId=Session::get('org_id');
$org_credentials=PaypalCredentials::where('org_id','=',orgId)->first();
$credentials=$org->paypalCredentials;
$this->_api_context = new ApiContext(new OAuthTokenCredential(
$credentials->client_id,
$credentials->client_secret));
$this->_api_context->setConfig($paypal_conf['settings']);
return $next($request);
});
}
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
解决方案
public function __construct()
{
/** PayPal api context **/
$this->middleware(function ($request, $next) {
$paypal_conf = \Config::get('paypal');
$orgID = $this->getSession('org_id');
$org_credentials=PaypalCredentials::where('org_id','=',orgId)->first();
$credentials=$org->paypalCredentials;
$this->_api_context = new ApiContext(new OAuthTokenCredential(
$credentials->client_id,
$credentials->client_secret));
$this->_api_context->setConfig($paypal_conf['settings']);
return $next($request);
});
}
private function getSession(){
return \Session::get('org_id');
}
推荐阅读
- flutter - 如何从 Flutter 获取堆栈跟踪?
- mysql - 将 2 个查询合二为一
- sql - 如果另一列为空,则禁止用户更新列?
- javascript - 在选择下拉菜单中更改占位符的文本颜色 - Material-ui
- c++ - C ++在无序树中查找具有最高值的节点
- webpack - 来自 UglifyJs Webpack 插件的错误 - 意外令牌:名称(扩展)
- python - 从外键中指定的模型中引用字段
- javascript - 想要创建一个映射或图表来显示多对多映射
- java - 如何检查某个用户是否具有对文档的阅读权限
- python - 等待 asyncio.sleep 中的 TypeError('dict' 对象不可调用)