ZF2 Route multiple strings to the same controller?

583 Views Asked by At

I want to configure my Zf2 application in such a way that multiple strings route to the same controller. For example www.mysite.com/this and www.mysite.com/that both route to the same controller and have this and that available to capture with $this->params. How would I accomplish something like this? Would I need 2 separate route declarations?

'directory' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string1 || /string2 || /string3',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index'
           ],
      ],
]
3

There are 3 best solutions below

0
On

As of definition of Literal route create 3 routes:

'directory1' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string1',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index',
           ],
      ],
],
'directory2' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string2',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index',
           ],
      ],
],
'directory3' => [
     'type'      => 'Zend\Mvc\Router\Http\Literal',
     'options'   => [
          'route'     => '/string3',
          'defaults'  => [
               'controller' => 'Application\Controller\MyController',
               'action'     => 'index',
           ],
      ],
],
0
On

Easiest solution IMO, is:

        'varcatcher' => [
            'type' => 'Segment',
            'options' => [
                'route' => '[/[:tail]]',
                'defaults' => [
                    'controller' => '\Application\Controller\Index',
                    'action' => 'catch',
                    'module' => 'Application',
                ],
                'constraints' => [
                    'tail' => '[a-zA-z0-9_-]*'
                ],
            ],
            'may_terminate' => true,
        ],

Then deal with it in your action:

public function catchAction(){
    die( $this->params()->fromRoute('tail') );
}

Because ZF2 routes are LIFO. It's probably optimal to deal with it by inserting it first, and handling whatever cases you need to 'catch'.

The mention of LIFO, is because if you define routes 'after' in the router array, those will precede the catch-all, which seems to be of benefit if I've read your question right.

Cheers! Alex

0
On

You could use a Zend\Mvc\Router\Http\Regex route type, instead of a Literal one and do something like

'directory' => [
    'type'      => 'Zend\Mvc\Router\Http\Regex',
    'options'   => [
        'route'     => '/string(?<id>[0-9]+)',
        'defaults'  => [
            'controller' => 'Application\Controller\MyController',
            'action'     => 'index'
        ],
    ],
]