Pergunta

I have developed API's with Zf2 and DynamoDB, I am able to get values from GET params in my local machine but unable to get values from GET params when I uploaded the API's in production . FYI: POST method is working properly in production.

Below is the controller get function.

public function get($id)
{
     $abcModel = new ABCModel();
     error_log("tournamentId:".$this->params()->fromQuery('tournamentId') );
     $query = $this->getRequest()->getQuery();
     error_log("tournamentId1:".$query['tournamentId']);
     error_log("tournamentId2:".$this->getEvent()->getRouteMatch()->getParam('tournamentId'));
     error_log("tournamentId3:".$this->params('tournamentId'));
     error_log("tournamentId4:".$this->params()->fromRoute('tournamentId'));
 }

I have tried all the answers of this question ZF2: Get url parameters in controller.

Can any one know what could be the reason for this?

Any light on the path would be helpful.

Foi útil?

Solução

To use query string in the production environment, you have to use some alternate approach. You can add parameter along with route to hold the query string value. But the query string needs to be passed using "/" mark between the route and query string instead of using "?" mark.

/route-name/key=value&key=value1

and the routing configuration need to be

'router' => array(
'routes' => array(
    '<route-name>' => array(
        'type' => 'segment',
        'options' => array(
            'route' => '<route-name>[/:action][/:queryPara][/]',
            'constraints' => array(
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'queryPara' => '[a-zA-Z][a-zA-Z0-9_-&=]*',
            ),
            'defaults' => array(
                'controller' => 'Application\Controller\Index',
                'action' => 'index',
            )
        ),
    ),
)),

You can create a function that will extract the query string and returns the array containing key=>value pairs of query string.

And in controller you have to call the function by passing the query string which will be stored in "/queryPara" part after the route name using the following statement you will get the string

$this->params('queryPara')

Hope it helps

Thanks

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top