title: Laravel Redirects and Validation Logic
body: <p>I'm using Laravel 4 and I'm looking for the elegant way of handling validation errors in my controller methods. Everything in the code below works fine (in the real code I actually validate.  My example just throws an exception to simulate a failed validation.) Basically, I want to get the try/catch block out of each controller and have it in another method so that I'm not repeating a lot of code that is very similar.  The code I have is as follows:</p>
  
  <p>routes.php</p>
  
  <pre><code>Route::resource( 'foo', 'FooController', array('only' =&gt; array('create', 'store')));
  </code></pre>
  
  <p>FooController.php</p>
  
  <pre><code>class FooController extends \BaseController {
  
      public function create()
      {
          return View::make('foo');
      }
  
      public function store(){
  
          try {
              // Validation service call goes here.  We'll just force an exception
              throw new Exception('Invalid email address!');
          } catch (Exception $e) {    
              return Redirect::back()-&gt;withErrors($e-&gt;getMessage());
          }
  
          // Everything validated so lets save the data   
      }
  }
  </code></pre>
  
  <p>foo.blade.php</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;h4&gt;Welcome to page Foo!&lt;/h4&gt;
  
      @if($errors-&gt;any())
      &lt;h4&gt;{{$errors-&gt;first()}}&lt;/h4&gt;
      @endif
  
      {{ Form::open(array('url' =&gt; '/foo')) }}
  
          {{ Form::label('email', 'E-Mail Address') }}
          {{ Form::text('email', 'example@gmail.com') }}
  
          {{ Form::submit('Submit') }}
      {{ Form::close() }}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>the above code works fine.  The exception from validation is thrown and we are redirected back to the page with the error message shown.</p>
  
  <p>What I want to do is move the whole try/catch block into a method to clear the clutter from the store() method.  Something like this:</p>
  
  <pre><code>class FooController extends \BaseController {
  
      public function create()
      {
          return View::make('foo');
      }
  
      public function store(){
  
          $this-&gt;validateMe();
  
          // Everything validated so lets save the data   
      }
  
      public function validateMe()
      {
          try {
              // Validation service call goes here.  We'll just force an exception to
              // simulate invalid data the sake of the example
              throw new Exception('Invalid email address!');
          } catch (Exception $e) {    
              return Redirect::back()-&gt;withErrors($e-&gt;getMessage());
          }
      }
  }
  </code></pre>
  
  <p>This implementation just gives me a blank screen because the redirect isn't making its way back to Laravel.  The only way to get redirected back to the form is to do something like below.  BUT, that won't work because I can never get to the logic that actually saves the data.</p>
  
  <pre><code>public function store(){
  
      return $this-&gt;validateMe();
  
      // Everything validated so lets save the data but we can 
      // never get here because of the return above.  This is bad!    
  }
  </code></pre>