我一直在寻找并尝试了一段时间,我无法找出我在做什么错... 我一直在阅读一些文档,并尝试过许多不同的方法。ZF2模块配置service_manager添加构造函数参数
我希望你们中的一个可以帮助我..
我的想法: 我有一个名为应用模块。在这个模块中有一个\Application\Controller\IndexController
这是用来显示我的应用程序的主页。 IndexController需要一个\Zend\http\Client
的对象,所以我应该使用\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();
}
}
错误:
Catchable fatal error: Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Zend\Http\Client, none given, called in /var/www/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /var/www/module/Application/src/Application/Controller/IndexController.php on line 13