Search code examples
phpapirestzend-framework2

ZF2 Apigility Rest - unable to get multiple params


I'm using Apigility to build my Rest APIs.

I want to build have the ability to take in multiple parameters. Such as

http://mydomain.com/users/1/activities/4

I now this is possible from this page: https://github.com/zfcampus/zf-apigility/issues/10

I have edit my module route to:

'route' => '/users/:users_id/activities[/:activities_id]',

But, I don't know how to retrieve users_id from the url.

I tried grabbing the params in my Resource php

    $evt = $this->getEvent();
    $params = $evt->getParams();
    die(var_dump($params));

But, that only returns

object(ArrayObject)#748 (1) {
  ["storage":"ArrayObject":private]=>
  array(1) {
    ["id"]=>
    string(1) "4"
  }
}

I'm a little baffled. Please advice.

Thanks!


Solution

  • In Zend Framework 2, you can use the RouteMatch to get the parameters of a route. For your case, you could try this :

        $e = $this->getEvent();
        $route = $e->getRouteMatch();
        $usr_id = $match->getParam('users_id');
    

    The user_id is now in the $usr_id variable.