code9monkey
code9monkey

Reputation: 101

zend framework 2 routing going to incorrect action

I am trying to hit the datatable action with the sID and cID params.

/CustomerManager/datatable/123/100

When the above url is hit it goes to the refreshdata see below image. enter image description here

Working fine when the url is

/CustomerManager/datatable

Can anyone see problem with the config?

'CustomerManager' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/CustomerManager[/:action]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'CustomerManager\Controller\CustomerManager',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'datatable' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/[:sID][/:cID]',
                        'constraints' => array(
                            'cono' => '[a-zA-Z0-9_-]+',
                            'cust' => '[a-zA-Z0-9_-]+',
                        ),
                        'defaults' => array(
                            'action' => 'datatable',
                        ),
                    ),
                ),
                'refreshdata' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route'    => '/[:showDel][/:sID][/:cID]',
                        'defaults' => array(
                            'action' => 'refreshdata'
                        ),
                        'constraints' => array(
                            'showDel' => '[a-zA-Z0-9_-]+',
                            'sID' => '[a-zA-Z0-9_-]+',
                            'cID' => '[a-zA-Z0-9_-]+',
                        )
                    )
                ),
            )

Upvotes: 0

Views: 127

Answers (2)

Wilt
Wilt

Reputation: 44316

After my comment an example on how you declare your routes more structured:

'router' => array(
    'CustomerManager' => array(
        'CustomerManager' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/CustomerManager',
                'defaults' => array(
                    'controller' => 'Application\Controller\IndexController',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // Literal route for data table
                'datatable' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/datatable',
                        'defaults' => array(
                            'action' => 'datatable',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        // Child route for table cell
                        'cell' => array(
                            'type' => 'segment',
                            'options' => array(
                                'route' => '/[:sID][/:cID]',
                                'constraints' => array(
                                    'sID'=> '[a-zA-Z0-9_-]+',
                                    'cID' => '[a-zA-Z0-9_-]+',
                                ),
                            ),
                        ),
                    ),
                ),
                // Literal route for refreshtable
                'refreshtable' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/refreshtable',
                        'defaults' => array(
                            'action' => 'refreshtable',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        // Child route for table cell
                        'cell' => array(
                            'type' => 'segment',
                            'options' => array(
                                'route' => '/[:sID][/:cID]',
                                'constraints' => array(
                                    'sID'=> '[a-zA-Z0-9_-]+',
                                    'cID' => '[a-zA-Z0-9_-]+',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

This is simply an example you can change it after your specific needs.


EDIT

Understanding the basics of routing is important for building a proper application. You should read the ZF2 Routing documentation thoroughly, all the information you need is in there...

For example:

In the question the sID and cID segments are optional (between square brackets). This will most likely also not work as you expect. A problem might occur when column cID is given but no sID value is given. The router will attach the cID value to the sID parameter name in such case, leading to unexpected route parameter values in your match.

Something like this could be solved by doing something like this with your constraints:

'constraints' => array(
    'sID' => 's[a-zA-Z0-9_-]+',
    'cID' => 'c[a-zA-Z0-9_-]+' 
),

By preceding the regular expression constraints (and the values in the url) with either an s or a c you make sure the correct value is tied to the correct parameter.

Upvotes: 2

Saeed.Gh
Saeed.Gh

Reputation: 1305

it seems "refreshdata" overwrite "datatable" because you expect a url with at last 3 parameter which all can be letters or numbers

and in request you call a url with two number type parameters which matches with "refreshdata"

also I think

'cono' => '[a-zA-Z0-9_-]+',
'cust' => '[a-zA-Z0-9_-]+',

in this line "cono" and "cust" must change to "sID" and "cID"

Upvotes: 2

Related Questions