Zend 框架 3:将参数传递给控制器

Zend framework 3: Pass arguments to controllers

我正在尝试通过控制器工厂在控制器中注入依赖项。我的 module.config.php 文件包含

<?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' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
           'getFilters' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/api/getFilters',
                    'defaults' => [
                        'controller' => Controller\Api\SearchController::class,
                        'action' => 'getFilters'
                    ]
                ]
            ],
   'controllers' => [
        'factories' => [
 Controller\Api\SearchController::class => function ($container) {
                return new Controller\Api\SearchController(
                 $container->get("first"),
                    $container->get("second")
                );
            },
            Controller\IndexController::class => function ($container) {
                return new Controller\IndexController();
            },
            Controller\Api\SearchController::class => InvokableFactory::class
        ]
        ]
// view_manager code

并且控制器文件 Controller\Api\SearchController 包含

<?php

namespace Commerce\Controller\Api;
class SearchController extends \Zend\Mvc\Controller\AbstractRestfulController
{

    public function __construct($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
    }
    public function getFiltersAction()
    {
       // some code
    }
}

Module.php代码

<?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->getApplication();
        $serviceManager = $application->getServiceManager();

        // The following line instantiates the SessionManager and automatically
        // makes the SessionManager the 'default' one.
        $sessionManager = $serviceManager->get(SessionManager::class);
    } 
}

虽然 运行 上面的代码说

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

我在这里缺少什么?在控制器中注入 parameter/value 依赖项的最佳方法是什么?

我成功了。在 module.config.php 文件中而不是使用

'controllers' => [
        'factories' => [
 Controller\Api\SearchController::class => function ($container) {
                return new Controller\Api\SearchController(
                 $container->get("first"),
                    $container->get("second")
                );
            },
 ]
]

我不得不使用

'controllers' => [
        'factories' => [
 Controller\Api\SearchController::class => function ($container) {
                return new Controller\Api\SearchController(
                "first",
                "second"
                );
            },
 ]
]