title: Laravel 5: How to handle code that is used in many Controllers
body: <p>I am new to Laravel and would like to know the best pracitse method to handle duplicate code.</p>
  
  <p>I have nearly in all functions of my api-controllers this code at beginning:</p>
  
  <pre><code>// Validate parameters
  $validator = Validator::make($request-&gt;all(), $this-&gt;validationRules);
  if ($validator-&gt;fails()) {
      return response()-&gt;json($validator-&gt;errors(), 422);
  }
  </code></pre>
  
  <p>So normally I would put it in some function. But this code also used in lot of other controllers. so this function should be called also by other controllers.</p>
  
  <p>What is best practise to handle that in Laravel 5? My first idea was to make some own Controller with this function and my other controllers would extend this.</p>
  
  <p>Like:</p>
  
  <pre><code>class MyController extends Controller
  {
      protected function handleFailByPrameter($params)
      {
           ....
      }
  }
  
  
  class Api1Controller extends MyController
  {
       public function apicall1()
       {
            $this-&gt;handleFailByPrameter($this-&gt;validationRules());
       }
  }
  </code></pre>
  
  <p>But maybe there are some other methods to handle that kind of things?</p>