I am having trouble building the factory in Zend Framework 3, it is displaying the following error.
Message:
Unable to resolve service "Users\AdapterInterface" to a factory; are you certain you provided it during configuration?
UsersController.php
<?php
class UsersController extends AbstractActionController {
private $table;
public function __construct(PostTable $table) {
if(!is_null($table))
$this->table = $table;
}
public function indexAction() {
$postsTable = $this->table;
return new ViewModel([
'posts' => $postsTable->fetchAll() // variavel posts passando o valor de posts
]);
}
}
module.config.php
<?php
namespace Users;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
],
'router' => [
'routes' => [
'users' => [
'type' => Segment::class,
'options' => [
'route' => '/users',
'defaults' => [
'controller' => Controller\UsersController::class,
'action' => 'index',
],
],
],
],
],
'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',
'users/users/index' => __DIR__ . '/../view/users/users/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
'users' => __DIR__ . '/../view',
],
],
];
Module.php
<?php
namespace Users;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface {
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) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Post());
return new TableGateway('post', $dbAdapter, null, $resultSetPrototype);
}
]
];
}
public function getControllerConfig() {
return [
'factories' => [
Controller\UsersController::class => function($container) {
return new Controller\UsersController($container->get(Model\PostTable::class));
}
]
];
}
}
Folder structure
Users
- config -> inside (module.config.php)
- src - > inside (Controller (UsersConstroller), Model, Module.php)
- test
- view