title: Zend framework 3: Pass arguments to controllers
body: <p>I'm trying to inject dependencies in a controller through controller factories.  My module.config.php file contains</p>
  
  <pre><code>&lt;?php
  
  namespace Commerce;
  
  use Commerce\Controller\Plugin\Website;
  use Zend\Router\Http\Literal;
  use Zend\Router\Http\Segment;
  use Zend\ServiceManager\Factory\InvokableFactory;
  
  return [
      'router' =&gt; [
          'routes' =&gt; [
              'home' =&gt; [
                  'type' =&gt; Literal::class,
                  'options' =&gt; [
                      'route'    =&gt; '/',
                      'defaults' =&gt; [
                          'controller' =&gt; Controller\IndexController::class,
                          'action'     =&gt; 'index',
                      ],
                  ],
              ],
             'getFilters' =&gt; [
                  'type' =&gt; Segment::class,
                  'options' =&gt; [
                      'route' =&gt; '/api/getFilters',
                      'defaults' =&gt; [
                          'controller' =&gt; Controller\Api\SearchController::class,
                          'action' =&gt; 'getFilters'
                      ]
                  ]
              ],
     'controllers' =&gt; [
          'factories' =&gt; [
   Controller\Api\SearchController::class =&gt; function ($container) {
                  return new Controller\Api\SearchController(
                   $container-&gt;get("first"),
                      $container-&gt;get("second")
                  );
              },
              Controller\IndexController::class =&gt; function ($container) {
                  return new Controller\IndexController();
              },
              Controller\Api\SearchController::class =&gt; InvokableFactory::class
          ]
          ]
  // view_manager code
  </code></pre>
  
  <p>and controller file Controller\Api\SearchController contains</p>
  
  <pre><code>&lt;?php
  
  namespace Commerce\Controller\Api;
  class SearchController extends \Zend\Mvc\Controller\AbstractRestfulController
  {
  
      public function __construct($a, $b)
      {
          $this-&gt;a = $a;
          $this-&gt;b = $b;
      }
      public function getFiltersAction()
      {
         // some code
      }
  }
  </code></pre>
  
  <p><strong>Module.php</strong> code</p>
  
  <pre><code>&lt;?php
  
  namespace Commerce;
  
  use Base\ModuleManager\Feature\ConfigProviderInterface;
  
  use Zend\Mvc\MvcEvent; use Zend\Session\SessionManager;
  
  class Module implements ConfigProviderInterface {
      public function getConfig()
      {
          return include __DIR__ . '/../config/module.config.php';
      }
  
      /**
       * This method is called once the MVC bootstrapping is complete.
       */
      public function onBootstrap(MvcEvent $event)
      {
          $application = $event-&gt;getApplication();
          $serviceManager = $application-&gt;getServiceManager();
  
          // The following line instantiates the SessionManager and automatically
          // makes the SessionManager the 'default' one.
          $sessionManager = $serviceManager-&gt;get(SessionManager::class);
      } 
  }
  </code></pre>
  
  <p>While running above code it says </p>
  
  <blockquote>
    <p>Too few arguments to function
    Commerce\Controller\Api\SearchController::__construct(), 0 passed in
    /var/www/html/zf3/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php
    on line 30 and exactly 2 expected</p>
  </blockquote>
  
  <p>What I'm missing here ? What is the best way to inject parameter/value dependencies in controllers?</p>