首页 > 解决方案 > 在 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,
];

标签: laravel

解决方案


    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');

}

推荐阅读