我正在创建一个CRUD应用程序示例,其中需要将我的ZF3应用程序连接到数据库
我的模型分别是Post.php和PostTable.php
namespace Post\Model;
/**
*
*/
class Post
{
protected $id;
protected $title;
protected $description;
protected $category;
public function exchangeArray($data){
$this->id = $data['id'];
$this->title = $data['title'];
$this->description = $data['description'];
$this->category = $data['category'];
}
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->titile;
}
public function getDescription(){
return $this->description;
}
public function getCategory(){
return $this->category;
}
}
namespace Post\Model;
use Zend\Db\TableGateway\TableGatewayInterface;
/**
*
*/
class PostTable
{
function __construct(TableGatewayInterface $tableGateway)
{
# code...
$this->tableGateway = $tableGateway;
}
public function fetchAll(){
return $tableGateway->select();
}
} Module.config.php
namespace Post;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/post[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];我的Module.php
namespace Post;
use Zend\Db\AdapterInterface;
use Zend\Db\TableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
const VERSION = '3.0.3-dev';
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig(){
return [
'factories' => [
Model\PostTable::class => function($container){
$tableGateway = $container->get(Model\PostTableGateway::class);
return new Model\PostTable($tableGateway);
},
Model\PostTableGateway::class => function($container){
$adapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Post);
return new tableGateway('post',$adapter,null,$resultSetPrototype);
}
],
];
}
public function getControllerConfig(){
return [
'factories' => [
controller\IndexController::class => function($container){
return new Controller\IndexController($controller->get(Model\PostTable::class));
}
]
];
}
}IndexController.php
namespace Post\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function __construct($table){
$this->table = $table;
}
public function indexAction()
{
return new ViewModel();
}
}在第30行的F:\Path\to\Project\vendor\zendframework\zend-servicemanager\src\Factory\InvokableFactory.php中传递的函数Post\Controller\IndexController::__construct(),0的参数太少,而第15行的F:\Path\to\Project\module\Post\src\Controller\IndexController.php:15恰好为1,这是我的ArgumentCountError异常
public function __construct($table){
$this->table = $table;
}我希望从数据库中获得结果集。但是构造函数会输出一个参数计数错误异常
发布于 2019-11-12 20:37:29
您的代码中有两个错误。
第一个是在module.config.php中,在键controllers下,必须删除旧的IndexController定义:
'router' => [
...
],
'controllers' => [
'factories' => [
// This one is obsolete, and you must remove it.
// Controller\IndexController::class => InvokableFactory::class,
],
],
'view_manager' => [
...
],由于您在Module.php类中声明了控制器,因此module.config.php中的controllers键不再是必需的,您可以将其删除:
'router' => [
...
],
'view_manager' => [
...
],第二个错误:在IndexController工厂中(在Module.php中)有一个(双重)错误。
第一个是输入错误的命名空间,而不是Controller.
$controller而不是$container来检索PostTable实例。public function getControllerConfig() {
return [
'factories' => [
Controller\IndexController::class => function($container) {
return new Controller\IndexController($container->get(Model\PostTable::class));
}
]
];
}https://stackoverflow.com/questions/58817971
复制相似问题