簡體   English   中英

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);
    } 
}

當運行上面的代碼時,它說

函數Commerce \\ Controller \\ Api \\ SearchController :: __ construct()的參數太少,在第30行的/var/www/html/zf3/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php中傳遞了0預期2

我在這里想念什么? 在控制器中注入參數/值相關性的最佳方法是什么?

我知道了。 在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"
                );
            },
 ]
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM