Cawa
Cawa

Reputation: 1299

How to query route zf2?

I have config for my rote:

  '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' => 'index',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'query' => array(
                    'type' => 'Query',
                ),
            ),
        )

adn why i trying to go admin/controller/some_action?id=234234234, i have an error:

A 404 error occurred
Page not found.
The requested URL could not be matched by routing.

what is wrong with my config?

Upvotes: 2

Views: 3413

Answers (2)

yechabbi
yechabbi

Reputation: 1881

If you define a route config with these parameters :

//...
'defaults' => array(
                    'controller' => 'index',
                    'action' => 'Index',
                   ),
//...

You need to make sure that you have your controller class associated to this key at the level of your controllers configuration:

'controllers' => array(
        'invokables' => array(
            'index' => 'Your\Controller\Namespace\YourController',
            //...
        ),
    ),

However, it recommended to define more "structured" keys (imagine you have different index controllers at the level of different modules ...) This can be easily achieved by adding a key corresponding to the controller namespace:

'defaults' => array(
                    'controller' => 'Index',
                    'action' => 'Index',
                    '__NAMESPACE__'=>'Your\Controller\Namespace'
                   ),

//...

//Controllers configuration

'controllers' => array(
        'invokables' => array(
            'Your\Controller\Namespace\Index' => 'Your\Controller\Namespace\YourController',
            //...
        ),
    ),

[EDIT]

Try with this (structured) config:

'admin' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '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(
                        ),
                    ),
                    'child_routes' => array(
                        'query'=>array(
                            'type'=>'Query',
                        )
                    )
                ),
            ),
        ),

Upvotes: 2

Andrew
Andrew

Reputation: 12809

Possibly your regex you have defined:

'controller' => '[a-zA-Z][a-zA-Z0-9_-]*/?',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*/?',

this is forcing you to match a trailing slash, try this:

'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',

Upvotes: 0

Related Questions