Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend framework 2 module name routing issue

I have a problem with module routing. I have 2 modules, Application and Admin. Each modules have indexAction as default action:

localhost/ --> Application/index

localhost/admin/ -> Admin/index

Admin/index works only with localhost/admin/index/

This problem happens when a module name starts with the letter "A". If I rename Admin to "Cars", localhost/cars/ works correctly!

the error is:

A 404 error occurred
The requested controller was unable to dispatch the request.
Controller:
Application\Controller\Application
No Exception available

This is module.config.php inside Application module:

<?php
return array(
    'router' => array(
         'routes' => array(
             'Application' => array(
                 'type'    => 'Segment',
                 'options' => array(
                     'route'    => '[/][:action/]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',                         
                     ),
                     'defaults' => array(
                         'controller' => 'Application\Controller\Application',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),
   'controllers' => array(
        'invokables' => array(
            'Application\Controller\Application' => 'Application\Controller\IndexController'
        ),
    ),

     'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'Application/Application/index' => __DIR__ . '/../view/Application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),  
        'template_path_stack' => array(
            'Application' => __DIR__ . '/../view',
        ),
    ),
);
?>

this is module.config.php inside Admin module:

    <?php
return array(
    'router' => array(
         'routes' => array(
             'Admin' => array(
                 'type'    => 'Segment',
                 'options' => array(
                     'route'    => '/admin/[:action/]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                     ),
                     'defaults' => array(
                         'controller' => 'Admin\Controller\AdminController',
                         'action' => 'index'
                     ),
                 ),
             ),
         ),
     ),
    'controllers' => array(
        'invokables' => array(
            'Admin\Controller\AdminController' => 'Admin\Controller\AdminController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'template_path_stack'      => array(
            'Admin' => __DIR__ . '/../view',
        ),
    ), 
);
?>

IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{

    public function indexAction(){

    }
}

AdminController.php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AdminController extends AbstractActionController
{

    public function indexAction()
    {}
}

Anyone can help me?

like image 638
Alessandro Corradini Avatar asked Nov 16 '15 15:11

Alessandro Corradini


Video Answer


1 Answers

First your error The requested controller was unable to dispatch the request. only occures when the router can't dispatch the request to its defined action. So please verify that your controller are correct and the actions are present and callable.

As allready pointed out your /admin/ route will point to two configured endpoints. This is not a problem in the first place when the admin route would be defined before the application route in the config.

So your route /admin/ route would never be routed to your AdminController as the other dynamic route would be matched first.

To get you expected result use the priority setting in your route, to make sure your Admin route will be matched before your Application route.

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

Beside your question, don't end php scripts with ?> as this could lead to bugs when a whitespace is after the end tag.

like image 136
ins0 Avatar answered Oct 17 '22 11:10

ins0