0

我一直在搜索和尝试一段时间,但我找不到我做错了什么......我现在已经阅读了很多文档并尝试了很多不同的方法。

我希望你们中的一个可以帮助我..

我的想法:我有一个名为 Application 的模块。在这个模块中有一个\Application\Controller\IndexController用于显示我的应用程序的主页。IndexController 需要一个带有 的对象\Zend\http\Client,所以我应该在返回的配置中使用 factory 键,\Application\Module->getServiceConfig()并添加一个带有所需参数的构造函数\Zend\Http\Client $client

不幸的是,这不起作用..

我的代码:

<?php
// module/Application/Module.php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'IndexController' => function($serviceManager) {
                    $httpClient = \Zend\Http\Client;

                    return new IndexController($httpClient);
                },
            ),
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

<?php
// module/Application/config/module.config.php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            )
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type' => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern' => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

<?php
// module/Application/src/Application/Controller/IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    protected $httpClient;

    public function __construct(\Zend\Http\Client $client)
    {
        $this->httpClient = $client;
    }

    public function getHttpClient()
    {
        return $this->httpClient;
    }

    public function indexAction()
    {                
        $httpClient = $this->getHttpClient();

        return new ViewModel();
    }
}

错误:

可捕获的致命错误:传递给 Application\Controller\IndexController::__construct() 的参数 1 必须是 Zend\Http\Client 的实例,没有给出,在 /var/www/library/Zend/ServiceManager/AbstractPluginManager.php 中调用170 并在第 13 行的 /var/www/module/Application/src/Application/Controller/IndexController.php 中定义

4

1 回答 1

3

您需要getControllerConfig在 Module.php 中实现该方法来执行您所要求的操作,控制器有自己的管理器,从 getServiceConfig 中删除您拥有的内容,并改用以下内容

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'Application\Controller\Index' => function($serviceManager) {
                $httpClient = new \Zend\Http\Client;
                return new \Application\Controller\IndexController($httpClient);
            },
        ),
    );
}

您还需要从 module.config.php 中的控制器可调用文件中删除该行,因为它会与您的工厂冲突

'controllers' => array(
    'invokables' => array(
        // this line's no longer needed
        //'Application\Controller\Index'      => 'Application\Controller\IndexController',
    ),
),
于 2013-03-08T15:34:35.447 回答