title: RESTful API response in Laravel 5
body: <p>I'm building RESTful API with Laravel. My API always returns JSON. What I would like to do, is keeping response logic at one place. Here's how I do it right now in API controller, which is pointed to by <code>Route::controller()</code>. Funny and ultra-useful example coming:</p>
  
  <pre><code>public function getDouble($number) {
      try {
          if (!is_numeric($number)) {
              throw new HttpException(400, 'Invalid number.');
          }
  
          $response = $number * 2;
          $status = 200;
      }
      catch (HttpException $exception) {
          $response = $exception-&gt;getMessage();
          $status   = $exception-&gt;getStatusCode();
      }
  
      return response()-&gt;json($response, $status);
  }
  </code></pre>
  
  <p>In this example, my API route would be for example <code>/double/13</code> accessed by GET method. The problem is that I repeat this try ... catch block in each method. I would like my API methods to be like:</p>
  
  <pre><code>public function getDouble($number) {
      if (!is_numeric($number)) {
          throw new HttpException(400, 'Invalid number.');
      }
  
      return $number;
  }
  </code></pre>
  
  <p>And then, catch those exceptions and form JSON in another place. What is the best approach here in terms of good application architecture?</p>