WJR
WJR

Reputation: 340

ZF2 Route To Controller at Top Level

Please excuse my newbiness to ZF2. I want to route to controller from top level...

In ZF2 Skeleton, it's setup so that route is segmented after application like this :
http://www.example.com/application[/:controller[/:action]]


But I would like to not go to thru application path and go to controller like this :
http://www.example.com/[:controller[/:action]]

I've searched extensively but just couldn't get it to work. I have my module.config.php setup as follow:

/* in module.config.php */

'router' => array(
    'routes' => array(
        'home' => array(
            'type'    => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),


'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Login' => 'Application\Controller\LoginController',
    ),
),

If I browse to http://www.example.com/login , it says "The requested URL could not be matched by routing." I think I'm missing fundamental concepts... =(

Upvotes: 1

Views: 174

Answers (2)

WJR
WJR

Reputation: 340

With guidance from Sam, I was able to figure it out :

'router' => array(
    'routes' => array(
        'home' => 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(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller' => 'Index',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

Upvotes: 0

Sam
Sam

Reputation: 16455

The child route gets concatenated to the parent route. This means ultimately you're defining the following possible route:

http://example.com//:controller/:action

Notice the double dipped forward slash. If all your child route parameters are optional and the namespace doesn't change, it makes no sense to define them as a child_route at all, only makes it slower in the end.

Upvotes: 1

Related Questions