title: How to structure route controllers in slim framework 3 (mvc pattern)
body: <p>I am working on a Slim Framework based API.  Following the mvc pattern, I want my routes to be controller driven with logger and renderer injected to every controller.</p>
  
  <p>As a start point I checked out a number of example mvc slim skeletons, and decided to base my structor on one particular tutorial and sample project ( <a href="http://jgrundner.com/slim-oo-004-controller-classes/" rel="nofollow">http://jgrundner.com/slim-oo-004-controller-classes/</a> )</p>
  
  <p>In this setup, injection is done by adding the router controllers to the app container like this:</p>
  
  <pre><code>$container = $app-&gt;getContainer();
  
  $container['\App\Controllers\DefaultController'] = function($c){
      return new \App\Controllers\DefaultController(
          $c-&gt;get('logger'),
          $c-&gt;get('renderer')
      );
  };
  </code></pre>
  
  <p>This then allow for a nice clean route and controller:</p>
  
  <p>route e.g.:</p>
  
  <pre><code>$app-&gt;get('/[{name}]', '\App\Controllers\DefaultController:index');
  </code></pre>
  
  <p>controller e.g.:</p>
  
  <pre><code>namespace App\Controllers;
  
  use Psr\Log\LoggerInterface;
  use Psr\Http\Message\RequestInterface;
  use Psr\Http\Message\ResponseInterface;
  
      class DefaultController{
          private $logger;
          private $renderer;
  
          public function __construct(LoggerInterface $logger, $renderer){
              $this-&gt;logger   = $logger;
              $this-&gt;renderer = $renderer;
          }
  
          public function index(RequestInterface $request, ResponseInterface $response, $args){
              // Log message
              $this-&gt;logger-&gt;info("Slim-Skeleton '/' route");
  
              // Render index view
              return $this-&gt;renderer-&gt;render($response, 'index.phtml', $args);
          }
  
          public function throwException(RequestInterface $request, ResponseInterface $response, array $args){
              $this-&gt;logger-&gt;info("Slim-Skeleton '/throw' route");
              throw new \Exception('testing errors 1.2.3..');
          }
      }
  </code></pre>
  
  <p>Extending the default controller keeps the controllers neat, but requires adding every new controller object to the app container first, which seems inefficient and messy when you have a lot of classes.</p>
  
  <p>Is there a better way?</p>