title: Persist Form Data Across Multiple Steps in Laravel
body: <p>When I've made multistep forms in the past I would generally store the form data in the session before returning it to the view, that way the data persists if the user refreshes the page or clicks the browser's native back buttons.</p>
  
  <p>Transferring my past logic to Laravel I built the following form consisting of three stages:</p>
  
  <p><code>[Input  -&gt;  Confirm  -&gt;  Success]</code></p>
  
  <h2>Routes.php</h2>
  
  <pre><code>Route::group(array('prefix' =&gt; 'account'), function(){
      Route::get('register', array(
          'before'  =&gt; 'guest',
          'as'      =&gt; 'account-create',
          'uses'    =&gt; 'AccountController@getCreate'
      ));
  
      Route::post('register', array(
          'before'  =&gt; 'guest|csrf',
          'as'      =&gt; 'account-create-post',
          'uses'    =&gt; 'AccountController@postCreate'
      ));
  
      Route::get('register/confirm', array(
          'before'  =&gt; 'guest',
          'as'      =&gt; 'account-create-confirm',
          'uses'    =&gt; 'AccountController@getCreateConfirm'
      ));
  
      Route::post('register/confirm', array(
          'before'  =&gt; 'guest|csrf',
          'as'      =&gt; 'account-create-confirm-post',
          'uses'    =&gt; 'AccountController@postCreateConfirm'
      ));
  
      Route::get('register/complete', array(
          'before'  =&gt; 'guest',
          'as'      =&gt; 'account-create-complete',
          'uses'    =&gt; 'AccountController@getCreateComplete'
      ));
  });
  </code></pre>
  
  <h2>AccountController.php</h2>
  
  <pre><code>&lt;?php
  class AccountController extends BaseController {
  
    private $form_session = 'register_form';
  
    public function getCreate() 
    {
        if(Session::has($this-&gt;form_session)) 
        {
            // get forms session data
            $data = Session::get($this-&gt;form_session);
  
            // clear forms session data
            Session::forget($this-&gt;form_session);
  
            // load the form view /w the session data as input
            return View::make('account.create')-&gt;with('input',$data);
        }
  
        return View::make('account.create');
    }
  
    public function postCreate() 
    {
        // set the form input to the session
        Session::set($this-&gt;form_session, Input::all());
  
        $validation_rules = array(
            'email'         =&gt; 'required|max:50|email|unique:users',
            'password'      =&gt; 'required|max:60|min:6',
            'password_conf' =&gt; 'required|max:60|same:password'                    
        );
  
        $validator = Validator::make(Input::all(), $validation_rules);
  
        // get forms session data
        $data = Session::get($this-&gt;form_session);
  
        // Return back to form w/ validation errors &amp; session data as input
        if($validator-&gt;fails()) {
          return  Redirect::back()-&gt;withErrors($validator);
        } 
  
        // redirect to the confirm step
        return Redirect::route('account-create-confirm');
    }
  
    public function getCreateConfirm() 
    {
        // prevent access without filling out step1
        if(!Session::has($this-&gt;form_session)) {
          return Redirect::route('account-create');
        }
  
        // get forms session data
        $data = Session::get($this-&gt;form_session);
  
        // retun the confirm view w/ session data as input
        return View::make('account.create-confirm')-&gt;with('input', $data);
    }
  
    public function postCreateConfirm() 
    {
        $data = Session::get($this-&gt;form_session);
  
        // insert into DB
        // send emails 
        // etc.
  
        // clear forms session data
        Session::forget($this-&gt;form_session);
  
        // redirect to the complete/success step
        return Redirect::route('account-create-complete');
    }
  
    public function getCreateComplete() {
        return View::make('account.create-complete');
    }
  }
  </code></pre>
  
  <h2>create.blade.php</h2>
  
  <pre><code>&lt;form action="{{ URL::route('account-create-post') }}" method="post"&gt;
  
      Email: &lt;input type="text" name="email" value="{{ (isset($input['email'])) ? e($input['email']) : '' }}"&gt;
      @if($errors-&gt;has('email'))
          {{ $errors-&gt;first('email') }} 
      @endif
      &lt;br /&gt;
  
      Password: &lt;input type="text" name="password" value=""&gt;
      @if($errors-&gt;has('password'))
          {{ $errors-&gt;first('password') }} 
      @endif
      &lt;br /&gt;
  
      Password Confirm: &lt;input type="text" name="password_conf" value=""&gt;
      @if($errors-&gt;has('password_conf'))
          {{ $errors-&gt;first('password_conf') }} 
      @endif     
      &lt;br /&gt;
  
      {{ Form::token() }}
  
      &lt;input type="submit" value="Confirm"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <h2>create-confirm.blade.php</h2>
  
  <pre><code>Email: {{ $input['email']; }}
  Password: {{ $input['password']; }}
  
  &lt;form action="{{ URL::route('account-create-confirm-post') }}" method="post"&gt;
      {{ Form::token() }}
      &lt;a href="{{ URL::previous() }}"&gt;return&lt;/a&gt; 
      &lt;input type="submit" name="submit_forward" value="Submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The above works fine, however I am wondering if this is the best way to approach multi-step forms in Laravel?</p>