Witam,
zacząłem naukę zenda 3 z książki: "Zend Framework 3 - poradnik programisty - Adam Omelak".
Wykonując krok po kroku napotkałem duży problem i niestety nie mogę nigdzie znaleźć rozwiązania. Dodałem moduł users. No i wchodząc na users dostaję błąd:
The requested controller could not be mapped to an existing controller class.
Controller:
Application\Controller\UsersController (resolves to invalid controller class or alias: Application\Controller\UsersController)
Rwę już włosy z głowy i dalej nie wiem co jest nie tak. Wstawię kod z paru plików, może czyjeś bystre oko dostrzeże problem.
zend/module/Application/config/module.config.php
<?php
namespace Application;
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' => '/application[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'users' => [
'type' => Segment::class,
'options' => [
'route' => '/users[/:action][/:id]',
'defaults' => [
'controller' => Controller\UsersController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => function($sm) {
$usersService = $sm->get('Application\Model\UsersTable');
return new Controller\IndexController($usersService);
}
],
],
'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',
//'users/index' => __DIR__ . '/../view/users/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
'base_path' => '/zend3/public/'
],
];
zend/module/Application/src/Controller/UsersController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\UsersTable;
use Application\Model\User;
use Application\Form\UserForm;
class UsersController extends AbstractActionController
{
private $usersTable = null;
public function __construct(UsersTable $usersTable)
{
$this->usersTable = $usersTable;
}
public function indexAction()
{
$view = new ViewModel();
$rows = $this->usersTable->getBy();
$view->setVariable('userRows', $rows);
return $view;
}
public function addAction(){
$request = $this->getRequest();
$userForm = new UserForm();
$userForm->get('submit')->setValue('Dodaj');
if (!$request->isPost()) {
return ['userForm' => $userForm];
}
$userModel = new User();
$userForm->setInputFilter($userModel->getInputFilter());
$userForm->setData($request->getPost());
if (!$userForm->isValid()) {
return ['userForm' => $userForm];
}
$userModel->exchangeArray($userForm->getData());
$this->usersTable->save($userModel);
return $this->redirect()->toRoute('users');
}
public function editAction()
{
$view = new ViewModel();
$userId = (int) $this->params()->fromRoute('id');
$view->setVariable('userId', $userId);
if (0 == $userId) {
return $this->redirect()->toRoute('users', ['action' => 'add']);
}
try {
$userRow = $this->usersTable->getById($userId);
} catch (\Exception $e) {
return $this->redirect()->toRoute('users', ['action' => 'index']);
}
$userForm = new UserForm();
$userForm->bind($userRow);
$userForm->get('submit')->setAttribute('value', 'Zapisz');
$request = $this->getRequest();
$view->setVariable('userForm', $userForm);
if (!$request->isPost()) {
return $view;
}
$userForm->setInputFilter($userRow->getInputFilter());
$userForm->setData($request->getPost());
if (!$userForm->isValid()) {
return $view;
}
$this->usersTable->save($userRow);
return $this->redirect()->toRoute('users', ['action' => 'index']);
}
public function deleteAction()
{
$userId = (int) $this->params()->fromRoute('id');
if (empty($userId)) {
return $this->redirect()->toRoute('users');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'Anuluj');
if ($del == 'Usuń') {
$userId = (int) $request->getPost('id');
$this->usersTable->delete($userId);
}
// przekierowanie do listy użytkowników
return $this->redirect()->toRoute('users');
}
return [
'id' => $userId,
'user' => $this->usersTable->getById($userId),
];
}
}
A tutaj wrzucam zdjęcie jak wygląda struktura katalogów
https://zapodaj.net/03cf395f65818.png.html
https://zapodaj.net/270a57a2e9d6b.png.html
https://zapodaj.net/ab3b7d1e3d0fb.png.html