0

I typically format my routes in ZF2 like so: /name/to/route

Now I have been doing the same thing with my api routes however I am finding that I am struggling to include data such as encoded urls or arrays.

Here is an example of such a route:

http://example.com/api/register/access/code/c102dea422fa4bb6958d77a29d9873d2/http%3A%2F%2Frouter-local.example.com%2Fapi%2Fdirectory

The following represents forward slashes and thus causes the route not to work: %3A%2F%2

I am thinking I should encode my route as such:

http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%example.com%2Fapi%2Fdirectory

How do you configure the module.config file to deal with this?

Currently it is set as such in apigility:

'api.rpc.register-access-code' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/api/register/access/code/:access_code/:route',
                    'defaults' => array(
                        'controller' => 'Api\\V1\\Rpc\\RegisterAccessCode\\Controller',
                        'action' => 'registerAccessCode',
                    ),
                ),
            ),

EDIT

I have encoded my routes to include GET parameters by doing the following:

    $url = "http://example.com/api/register/access/code/";
    $params = [
        'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
        'route' =>  'http://example.com/api/directory'
    ];

    $final = $url . "?" . http_build_query($params);

Which gives this:

http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%2Fexample.com%2Fapi%2Fdirectory

However this breaks due to a "The requested URL could not be matched by routing." error.

The route is unidentified due to the interpretation of the slashes in the included URL.

Perhaps the issue is to do with how the URL is formatted and included as a parameter?

HappyCoder
  • 5,985
  • 6
  • 42
  • 73
  • You probably want to take a look at the [zf2 manual](http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html). `'Segment'` is only one of several types of routes, and you should review what they each do to determine which fit your needs. – jcropp Apr 24 '15 at 08:01

2 Answers2

1

You don't define query variables in the segment route option; only the path.

You may append ?query=vars to any url, regardless of route configuration. ZF2's url helpers should encode the query vars for you, you just have to create an array of query vars and give it to the helper function when creating a url.

<?php echo $this->url('api.rpc.register-access-code', array(), array('query' => array(
    'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
    'route' => 'http://router-local.example.com/api/directory',
))); ?>`
Emery King
  • 3,550
  • 23
  • 34
0

In this case, it would seem the problem is to do with htaccess or apache. The simplest solution has been to encode the url using: base64_encode($url) which can be de-coded at the other end.

HappyCoder
  • 5,985
  • 6
  • 42
  • 73