Question

Hello I am using ZendFramework 2.0 and I would like to set routing with query params. For example I would like something like that to get working.

I want route that will match .../foo?my_param=number but will not match .../foo or .../foo?not_allowed_param=value

'type' => 'Literal',
'options' => array(
    'route' => 'foo',
    'defaults' => ...// Route to some error handler
    'may_terminate' => true,
    'child_routes' => array(
        'query' => array(
             // there is some query so route to my action
            'type' => 'Query',
            'options' => array(
                'defaults' => array(
                    'controller' => 'index',
                    'action' => 'fooAction',
                ),
            ),
        ),
    ),
),

On the other side I want be able to use $this->url('.../foo', array('my_param' => 3))

Ofc that this does not work. I hope you get the idea what behaviour I expect.

Thanks for any help!

Était-ce utile?

La solution

Your answer suggests use of GET parameters. You could check in the controller if the variables have been passed.

If you want to do it through the router (as Sam notes) you can set up a segment style router. This is explained at this link: http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html

An example of how this could look is included below:

'YourName' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/property/search[/:action]',
                'constraints' => array(
                    'controller'=>'[a-zA-Z][a-zA-Z0-9_-]*',            
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'Module\Controller\Controller',
                    'action' => 'index',
                ),
            ),
        ),

Segments in square brackets are considered optional.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top