ZF2 routing ignores namespace

I have a problem with zf2 routing. I use the skeletal example https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php , but when I contact to access http://localhost/admin/or http://localhost/admin/answers, I get a 404 message with the message:

A 404 error occurred

Page not found.

The requested controller could not be mapped to an existing controller class.

Controller:
Index(resolves to invalid controller class or alias: Index)
No Exception available

From the error message, I think the router is ignoring __NAMESPACE__. Maybe someone can help me find a solution to my problem?

I used https://github.com/zendframework/ZFTool to create the module and controllers. My file structure:

module/Admin
β”œβ”€β”€ config
β”‚   └── module.config.php
β”œβ”€β”€ Module.php
β”œβ”€β”€ src
β”‚   └── Admin
β”‚       └── Controller
β”‚           β”œβ”€β”€ AnswersController.php
β”‚           └── IndexController.php
└── view
    └── admin
        β”œβ”€β”€ answers
        β”‚   └── index.phtml
        └── index
            └── index.phtml

My module.config.php:

return array(
    'controllers'  => array(
        'invokables' => array(
            'Admin\Controller\Answers' => 'Admin\Controller\AnswersController',
            'Admin\Controller\Index'   => 'Admin\Controller\IndexController',
        ),
    ),
    'router'       => array(
        'routes' => array(
            'admin' => array(
                'type'          => 'literal',
                'options'       => array(
                    'route'    => '/admin',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Admin\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'default' => array(
                        'type'    => 'segment',
                        'options' => array(
                            'route'       => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults'    => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'admin' => __DIR__ . '/../view',
        ),
    ),
);

If I change: '\__NAMESPACE__' => 'Admin\Controller', 'controller' => 'Index',- 'controller' => 'Admin\Controller\Index'I can access the index controller, but not the answers.

Additional Information:

. , . localhost/application/index localhost/application/answers, . ( ), config . - ? , ?

Zend Framework 2.2.4.

+4
3

'answers' => array(
  'type' => 'Zend\Mvc\Router\Http\Literal',
  'options' => array(
    'route'    => '/answers',

    'defaults' => array(
      'controller' => 'Admin\Controller\Answers',
      'action'     => 'index',
    ),
  ),
),

, , ?

'application' => array( 
    'type'    => 'segment', 
    'options' => array( 
        'route'    => '/[:controller][/:action][/:id]', 
        'constraints' => array(
            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*', 
            'id'         => '[0-9]+', 
        ), 
        'defaults' => array( 
            'controller' => 'Application', 
            'action'     => 'index',
        ), 
    ), 
    'may_terminate' => true, 
    'child_routes' => array( 
        'default' => array( 
            'type'    => 'Wildcard', 
            'options' => array( 
            ), 
        ), 
    ), 
), 

+1

Module.php

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
}
+1

100% , , . , :

'router' => array(
    'routes' => array(
        'admin' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/admin[/:controller[/:action]]',
                'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Admin',
                    'action'     => 'index',
                    ),
                ),
            ),
        ),
     ),

, .

return $this->redirect()->toRoute('admin', array('action' => 'newAction', 'controller' => 'someOtherController'));

URL-:

$this->url('admin', array('action' => 'newAction', 'controller' => 'someOtherController'));

EDIT:

Zend framework2 . Router .

, , , , . "" zf2.

EDIT2: I forgot to add some kind of route restriction, can you try it with the updated route configuration again?

enter image description here

0
source

Source: https://habr.com/ru/post/1530564/


All Articles