这是Rob Allen的Zend Framework beta4快速入门教程。
错误Message:Zend\ServiceManager\ServiceManager::get无法获取或创建相册表的实例
它似乎尝试连接到数据库失败,但我还没有找到方法来判断。它使用闭包从ServiceManager返回一个实例,但得到了上面的错误消息。
模块/相册/Module.php
命名空间相册;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfiguration()
{
$albumTable = array(
'factories' => array(
'album-table' => function($sm) {
$dbAdapter = $sm->get('db-adapter');
$table = new AlbumTable($dbAdapter);
return $table;
},
),
);
return $albumTable;
}
}命名空间应用;
使用Zend\Db\Adapter\Adapter作为DbAdapter,
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfiguration()
{
$factoryDBAdaptor = array(
'factories' => array(
'db-adapter' => function($sm) {
$config = $sm->get('config');
$config = $config['db'];
$dbAdapter = new DbAdapter($config);
return $dbAdapter;
},
),
);
return $factoryDBAdaptor;
}
}config\autoload\global.php
return array(
'db' => array(
'driver' => 'PDO',
'dsn' => 'mysql:dbname=zf2tutorial;hostname=localhost',
'username' => 'user',
'password' => 'password',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
);发布于 2012-07-06 19:39:48
这与Zend框架的主版从Beta 4开始更改的事实有关,因此我的针对beta 4的教程不再适用于最新的ZF主版。
此外,SM可能有以前的异常,因此您应该检查是否有任何以前的异常,因为这可能会显示潜在的错误。
更新
从2012年7月11日起,我的应用程序已经针对Beta5进行了更新,它现在使用Db适配器的ServiceFactory来创建适配器,因此您甚至不再需要修改tutorial的模块类。
发布于 2012-07-06 21:30:23
确保主Module.php有一个引用getServiceConfiguration()。我也有同样的问题,但忘了把它包括进去。
module/Application/Module.php:
<?php
namespace Application;
use Zend\Db\Adapter\Adapter as DbAdapter;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfiguration()
{
return array(
'factories' => array(
'db-adapter' => function($sm) {
$config = $sm->get('config');
$config = $config['db'];
$dbAdapter = new DbAdapter($config);
return $dbAdapter;
},
),
);
}
}发布于 2012-07-24 21:07:18
使用以下行更新您的composer.json文件。
"zendframework/zendframework": "dev-master#18c8e223f070deb07c17543ed938b54542aa0ed8"运行下面的命令,你就可以运行了。
php composer.phar self-update
php composer.phar update
php composer.phar installhttps://stackoverflow.com/questions/11355126
复制相似问题