在Laravel中作为会话闪存数据传递验证:不允许对闭包进行序列化


Passing validation as session flash data in Laravel: Serialization of Closure is not allowed

取以下类:

class SessionHelper
{
    public static function GetViewModel()
    {
        return unserialize( Session::get( 'viewModel' ) );
    }
    public static function StoreViewModel( $object )
    {
        return Session::flash( 'viewModel', serialize( $object ) );
    }
}

想象一下,我在中有一个注册控制器方法

public function PostRegister()
{
    $validator = Validator::make(
        $values,
        $rules,
        $messages
    );
    if( $validation->fails() )
    {
        $viewModel->User->Password = NULL;
        $viewModel->Validation = $validation->messages();                
        SessionHelper::StoreViewModel( $viewModel );
        return Redirect::action( "AuthenticationController@GetRegister" );
    }
}

它给出以下错误:

      Serialization of 'Closure' is not allowed 
      Open: C:'xampp'htdocs'...'app'models'Helpers'SessionHelper.php  
5.    public static function GetViewModel()
6.    {
7.        return unserialize( Session::get( 'viewModel' ) );
8.    }
9.    
10.    public static function StoreViewModel( $object )
11.    {
12.        return Session::flash( 'viewModel', serialize( $object ) );
13.    }
14.}

我希望能够使用Redirect::action函数将验证对象传递到另一个控制器方法中。。。你知道我该怎么做吗?

public function GetRegister()
{
    if( !$viewModel = SessionHelper::GetViewModel() )
    {
        $viewModel = new RegisterViewModel();
    }        
    return View::make( "Authentication/Register", ModelHelper::Prepare( $viewModel ) );
}

您可能只需要序列化消息数组:

$viewModel->Validation = $validation->messages()->all();