php – 我可以在Laravel 5会话中保存对象,对象集合吗?

前端之家收集整理的这篇文章主要介绍了php – 我可以在Laravel 5会话中保存对象,对象集合吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有可能在laravel 5 Session中保存PHP对象,对象集合?

我在尝试,但我得到了错误

Serialization of 'Closure' is not allowed vendor/illuminate/session/Store.PHP line 255

Session::put('my_PHP_object',$obj );
Session::save();


public function onRun()
{

    $this->addCss('assets/css/custom.css');
    $this->socialite_providers = $this->page['socialite_providers'] =$this->providersList();

    //check for provider param in url
    if($provider = $this->param('provider')){

        $this->setSessionProvider($provider);
        $this->provider = $provider;
        $this->callback_url = preg_replace('~.*\K:(.*)~s','',Request::root().$this->page->url);
        $this->request = $this->createRequest($provider);


        Session::save();
        return $this->request->redirect();

    }

    //Authorize user if Request has code
    if(Request::has('code')){

        if(!$this->getSession())
            return;

        //reuse save session

    }

}

public function createRequest($provider)
{
    $instance = Socialite::driver($provider);
    $init = $this->injectCredentials($instance,$provider);

    $this->setSession($init);
    return $init;
}

public function injectCredentials($instance,$provider){
    $credential = $this->providerData($provider)->toArray();
    $instance = new $instance
    (
        Request::instance(),$credential['client_id'],$credential['client_secret'],$this->callback_url
    );

    return $instance;
}

public function setSession($init)
{
    if(Session::has('socialite_object'))
        Session::forget('socialite_object');


    Session::put('socialite_object',$init );

}
您始终可以存储您尝试存储的对象的JSON表示.

但是,我听说不要在会话中存储大量内容通常是一个好主意,无论如何你可以通过存储其他内容来管理吗?如果要从数据库存储对象,则可以存储Id并使用查询恢复对象.

猜你在找的Laravel相关文章