我正在完成 ZF2 初学者深入教程,当我重新加载页面时,我不断收到以下错误消息:
“执行过程中发生错误,请稍后重试。
其他信息:
Zend\ServiceManager\Exception\ServiceNotCreatedException 文件:C:\xampp\htdocs\zend\供应商\zendframework\zendframework\库\Zend\ServiceManager\ServiceManager.php:946 消息:创建“Blog\Controller\List”时引发异常;没有返回实例”。
我已经达到了教程中的阶段,我准备了数据库,添加了映射器实现,并更改了 module.config.php 文件中的 Controller 管理器,以便它支持工厂。我似乎无法找出问题所在。我的代码摘录如下:
模块.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();
}
}
由于最下面一行代码中有一个 dump 命令,结果变量的数据转储应该返回类似:
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
}
但是我得到了如上所述的错误页面。
最佳答案
我发现了这个问题。本教程缺少这部分。
教程只说:
As you know from previous chapters, whenever we have a required parameter we need to write a factory for the class. Go ahead and create a factory for our mapper implementation.
您需要为 ZendDbSqlMapper 创建一个工厂: 创建/Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php
<?php
// Filename: /Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php
namespace Blog\Factory;
use Blog\Mapper\ZendDbSqlMapper;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ZendDbSqlMapperFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
*
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new ZendDbSqlMapper($serviceLocator->get('Zend\Db\Adapter\Adapter'));
}
}
关于php - 服务未创建异常: ZF2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30372878/