5

I've built my first RESTful API ever and used Slim as my framework. It works well so far.

Now I have seen a great API Design Guide which explained, the best way to build an API is to keep the levels flat. I want to do that and try to figure out how to build an URI like this:

my-domain.int/groups/search?q=my_query

The /groups part already works with GET, POST, PUT, DELETE and also the search query works like this:

my-domain.int/groups/search/my_query

This is the code I use for the routing in PHP:

$app->get('/groups/search/:query', 'findByName');

I just can't figure out how to build optional parameters with an question mark in Slim. I wasn't able to find anything on Google.

EDIT: Since the search not seems to be suitable for my scenario I try to show another way of what I want to realize:

Let's say I want to get a partial response from the API. The request should look like that:

my-domain.int/groups?fields=name,description

Not like that:

my-domain.int/groups/fields/name/description

How do I realize that in the routing?

Raphael
  • 265
  • 4
  • 11
  • Possible duplicate: [http://stackoverflow.com/questions/8125064/slim-php-and-get-parameters](http://stackoverflow.com/questions/8125064/slim-php-and-get-parameters) – TPete Jul 03 '14 at 08:18

4 Answers4

6

The parameters supplied with the query string, the GET parameters, don't have to be specified in the route parameter. The framework will try to match the URI without those values. To access the GET parameters you can use the standard php approach, which is using the superglobal $_GET:

$app->get('/groups/test/', function() use ($app) {
    if (isset($_GET['fields']){
        $test = $_GET('fields');
        echo "This is a GET route with $test";
    }
});

Or you can use the framework's approach, as @Raphael mentioned in his answer:

$app->get('/groups/test/', function() use ($app) {
    $test = $app->request()->get('fields');
    echo "This is a GET route with $test";
});
TPete
  • 2,049
  • 4
  • 24
  • 26
4

Ok I found an example that does what I need on http://help.slimframework.com/discussions/problems/844-instead

If you want to construct an URI Style like

home.int/groups/test?fields=name,description

you need to build a rout like this

$app->get('/groups/test/', function() use ($app) {
    $test = $app->request()->get('fields');
    echo "This is a GET route with $test";
});

It echoes: This is a GET route with name,description

Even though it's not an array at least I can use the question mark. With Wildcards I have to use /

Raphael
  • 265
  • 4
  • 11
1

You may also have optional route parameters. These are ideal for using one route for a blog archive. To declare optional route parameters, specify your route pattern like this:

<?php
$app = new Slim();
$app->get('/archive(/:year(/:month(/:day)))', function ($year = 2010, $month = 12, $day = 05) {
    echo sprintf('%s-%s-%s', $year, $month, $day);
});

Each subsequent route segment is optional. This route will accept HTTP requests for:

/archive
/archive/2010
/archive/2010/12
/archive/2010/12/05

If an optional route segment is omitted from the HTTP request, the default values in the callback signature are used instead.

manish1706
  • 1,571
  • 24
  • 22
0

Search query is not suitable for url parameters, as the search string might contain url separator (/ in your case). There's nothing wrong to keep it as query parameter, you don't have to push this concept everywhere.

But to answer your question, optional parameters are solved as another url:

$app->get('/groups/search/:query', 'findByName');
$app->get('/groups/search/strict/:query', 'findByNameStrict');

EDIT: It seems you want to use Slim's wildcard routes. You just need to make sure there's only one interpratation of the route.

$app->get('/groups/fields/:fields+', 'getGroupsFiltered');

Parameter $fields will be an array.

Marek
  • 7,337
  • 1
  • 22
  • 33
  • Thanks, but that doesn't solve my Problem. Even thought the search might not be suitable as scenario. how would I be able to build an URI with question mark, as mentioned above? What you suggested is what I already have. – Raphael Jun 16 '14 at 11:08