我已经搜索和尝试了一段时间了,我找不到我做错了什么…我现在已经阅读了相当多的文档,并尝试了很多不同的方法。
我希望你们中有人能帮我一把。
我的想法:我有一个叫做Application的模块。在这个模块中有一个'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();
}
}
错误:
可捕获的致命错误:参数1传递给Application'Controller'IndexController::__construct()必须是Zend'Http'Client的一个实例,没有给出,在/var/www/library/Zend/servicemanager/AbstractPluginManager.php第170行调用,并在/var/www/module/Application/src/Application/Controller/IndexController.php第13行定义
你需要在Module.php中实现getControllerConfig
方法来做你想做的事情,控制器有自己的管理器,从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',
),
),