gpt4 book ai didi

php - Zend\ServiceManager\ServiceManager::get 无法获取或创建 getAlbumTable 的实例

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:55 25 4
gpt4 key购买 nike

我正在尝试修改和复制一个自定义模块,我已经设置了所有数据库连接,但是在按如下方式查看我的模块时出现错误:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable

这是我的 module.config 文件:

return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),


// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),

'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);

这里是global.php中的数据库连接

return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=stickynotes;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),

'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);

这是服务配置的 module.php 中的代码:

 public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}

这里是获取相册的 Controller :

   <?php 

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


class AlbumController extends AbstractActionController
{
protected $_albumTable;

public function indexAction()

{
return new ViewModel(array(

'albums' => $this->getAlbumTable()->fetchAll(),

));
}

}

?>

这是数据库表的附件: Database tables View

任何人都可以让我知道我必须在哪里调试这个错误并解决问题吗?

最佳答案

当您调用 $this->getAlbumTable() 时,Zend 会寻找 Controller 插件,但没有具有此类名称的插件,因此它会抛出异常。

如果你想从 service_manager 访问任何类,你必须通过工厂注入(inject)它。

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use MyNamespace\Controller\AlbumController;

class AlbumControllerFactory implements FactoryInterface
{
/**
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return AlbumController
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$class = $requestedName ? $requestedName : AlbumController::class;
$albumTable = $container->get('Album\Model\AlbumTable'); // get service from service manager
$controller = new $class($albumTable);

return $controller;

}
/**
* Provided for backwards compatibility; proxies to __invoke().
*
* @param ContainerInterface|ServiceLocatorInterface $container
* @return AlbumController
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, AlbumController::class);
}
}

module.config.php

'controllers' => array(
'factories' => array(
Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class,
),
),

在你的 Controller 中:

   namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


class AlbumController extends AbstractActionController
{

protected $_albumTable;

public function __construct(AlbumTable $albumTable)
{
$this->_albumTable = $albumTable;
}

public function indexAction()
{
return new ViewModel(array(
'albums' => $this->_albumTable->fetchAll()

));
}
}

很简单,不是吗? :)

关于php - Zend\ServiceManager\ServiceManager::get 无法获取或创建 getAlbumTable 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com