【发布时间】:2015-08-03 01:41:26
【问题描述】:
我正在为初学者完成 ZF2 深度教程,当我重新加载页面时,我不断收到以下错误消息:
"执行时出错,请稍后重试。
附加信息:
Zend\ServiceManager\Exception\ServiceNotCreatedException 文件:C:\xampp\htdocs\zend\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:946 消息:创建“Blog\Controller\List”时引发异常;没有返回实例”。
我已经到了教程的阶段,我准备了数据库,添加了映射器实现,并在 module.config.php 文件中更改了我们的控制器管理器,以便它支持工厂。我似乎无法发现问题所在。我的代码摘录如下:
module.config.php:
// Tells our module where to find the view files. Can also overwrite view files for other modules.
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
),
// Tells our module where to find the controller named Blog\Controller\List
'controllers' => array(
'factories' => array(
'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
)
),
ListController.php:
<?php
// Filename: /module/Blog/src/Blog/Controller/ListController.php
namespace Blog\Controller;
use Blog\Service\PostServiceInterface;
use Zend\Mvc\Controller\AbstractActionController;
class ListController extends AbstractActionController
{
/**
* @var \Blog\Service\PostServiceInterface
*/
protected $postService;
public function __construct(PostServiceInterface $postService)
{
$this->postService = $postService;
}
public function indexAction()
{
return new ViewModel(array(
'posts' => $this->postService->findAllPosts()
));
}
}
?>
ListControllerFactory.php
<?php
namespace Blog\Factory;
use Blog\Controller\ListController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ListControllerFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
*
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');
return new ListController($postService);
}
}
ZendDBSQLMapper.php - 设计为映射器类来执行对现有数据库的调用:
<?php
// Filename: /module/Blog/src/Blog/Mapper/ZendDbSqlMapper.php
namespace Blog\Mapper;
use Blog\Model\PostInterface;
use Zend\Db\Adapter\AdapterInterface;
class ZendDbSqlMapper implements PostMapperInterface
{
/**
* @var \Zend\Db\Adapter\AdapterInterface
*/
protected $dbAdapter;
/**
* @param AdapterInterface $dbAdapter
*/
public function __constrcut(AdapterInterface $dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
/**
* @param int|string $id
*
* @return PostInterface
* @throws \InvalidArgumentException
*/
public function find($id)
{
}
/**
* @return array|PostInterface[]
*/
public function findAll()
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('posts');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
\Zend\Debug\Debug::dump($result);die();
}
}
由于最底行的代码中有一个转储命令,结果变量的数据转储应该返回如下内容:
object(Zend\Db\Adapter\Driver\Pdo\Result)#303 (8) {
["statementMode":protected] => string(7) "forward"
["resource":protected] => object(PDOStatement)#296 (1) {
["queryString"] => string(29) "SELECT `posts`.* FROM `posts`"
}
["options":protected] => NULL
["currentComplete":protected] => bool(false)
["currentData":protected] => NULL
["position":protected] => int(-1)
["generatedValue":protected] => string(1) "0"
["rowCount":protected] => NULL
}
但是我得到了如上所述的错误页面。
【问题讨论】:
-
这个错误是因为ZF2无法创建你的控制器的实例,所以调查你的控制器工厂。你确定
$postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');有效吗? -
我有同样的问题,发现错误发生在 Svengali 询问的行中: $postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');但不能说到底是什么
-
(不仅针对这种情况)如果 createService() 函数中的任何位置或此函数中调用的函数中的任何位置发生任何错误,这将返回(例如:$options = new MyOptions( $paramsConfig); ) 将导致返回此消息。因此,您最好逐行导航以找出问题的确切位置。
标签: php zend-framework zend-framework2