• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Zend 3 - po dodaniu modułu brak routingu

VPS Starter Arubacloud
0 głosów
295 wizyt
pytanie zadane 6 grudnia 2017 w PHP przez Alterwar Dyskutant (7,650 p.)

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

1 odpowiedź

0 głosów
odpowiedź 12 stycznia 2018 przez divix Nowicjusz (140 p.)

Zapomniałeś zdefiniować UsersController w pliku konfiguracyjnym: modules.config.php wewnątrzn klucza controllers -> factories:

    'controllers' => [
        'factories' => [
            Controller\IndexController::class => function($sm) {
                $postService = $sm->get('Application\Model\UsersTable');

                return new Controller\IndexController($postService);
            },
            Controller\UsersController::class => function($sm) {
                $postService = $sm->get('Application\Model\UsersTable');

                return new Controller\UsersController($postService);
            }
        ],
    ],

 

Podobne pytania

0 głosów
0 odpowiedzi 85 wizyt
pytanie zadane 4 lutego 2022 w PHP przez pvalue Dyskutant (8,720 p.)
0 głosów
1 odpowiedź 102 wizyt
pytanie zadane 11 grudnia 2020 w PHP przez pvalue Dyskutant (8,720 p.)
0 głosów
1 odpowiedź 181 wizyt
pytanie zadane 24 lutego 2018 w PHP przez Maniek Bywalec (2,170 p.)

92,454 zapytań

141,263 odpowiedzi

319,099 komentarzy

61,854 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Akademia Sekuraka 2024 zapewnia dostęp do minimum 15 szkoleń online z bezpieczeństwa IT oraz dostęp także do materiałów z edycji Sekurak Academy z roku 2023!

Przy zakupie możecie skorzystać z kodu: pasja-akademia - użyjcie go w koszyku, a uzyskacie rabat -30% na bilety w wersji "Standard"! Więcej informacji na temat akademii 2024 znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...