pavkatar
pavkatar

Reputation: 53

Dynamic key=>value segments in Zend Router

Need to achieve dynamic router param segmenting in Zend Router. The idea is: have the url: /route/:route/resource/:resource/:identifier, with the following configuration:

    'orchestration.rest.dynamic-router' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/route/:route/resource/:resource[/:identifier]',
                    'defaults' => array(
                        'controller' => 'Controller',
                    ),
                ),
            ),

Need to make it support n-number different key=>value router params in the following format:

/route/:route/resource/:resource/:identifier/key1/value1/key2/value2/key3/value3

The second problem is that this should work only if you have the optional :identifier parameter provided.

This is what I've checked, but not sure how to achieve the goal: https://docs.zendframework.com/zend-router/routing/#zend92router92http92segment

Upvotes: 0

Views: 68

Answers (1)

Eric
Eric

Reputation: 608

Leave static routes.

1) They are cached for better performance
2) if you use rest logic, use POST method, not GET(params in url)

the route

'orchestration.rest' => array(
                'type' => 'Segment',
                'options' => array(
                    'routerest' => '/routerest/:action',
                    'defaults' => array(
                        'controller' => 'Controller',
                    ),
                ),
            ),

use post to consume rest action on server, with Ajax, or other...

 (pseudo code)
 datas={key1 : param1 , key2 : param2, etc...}
   url=yourDomain/routerest/show   (show=the action)
   send Ajax Request(or something else in another langage with METHOD=POST)

other action like "getdatas"

 ... use post to consume rest action on server, with Ajax, or other...
   datas={id1 : param1 , id2 : param2, etc...}
   url=yourDomain/routerest/getdatas   (getdatas=the action)

... in your Controller.php
...handle show action in route

  function showAction()  {
      $request = $this->getRequest();
      if ($request->isPost()) {
            $param1=$this->params()->fromPost('key1','defaultvalue');
            $param2=$this->params()->fromPost('key2','defaultvalue');
            ...
            ...
      } else {... ERROR..}
   }

...handle getdatas action in route

 function getdatasAction()  {
      $request = $this->getRequest();
      if ($request->isPost()) {
            $id1=$this->params()->fromPost('id1','defaultvalue');
            $id2=$this->params()->fromPost('id2','defaultvalue');
            ...
            ...
            if ($id1$=='all') { return $this->redirect()->toRoute ('otherroute', array ('action' => 'xxx', 'paramxxx' => 'xx'));
            ...
            ...
      } else {... ERROR..}
   }

Upvotes: 0

Related Questions