0

I have a bunch of routes and they all start with /api/2.01.

How can I add it once so it applies to all routes. Slim Framework Base URL asks the same question, but I believe provides an outdated answer.

PS. If instead of asking a new question, should I have instead somehow tagged the post which I believe is dated to be reviewed or something?

$app = new \Slim\Slim();
$app->post('/api/2.01/books', function () {
    //Create books
});
$app->get('/api/2.01/books', function () {
    //getbook
});
$app->get('/api/2.01/books/{id}', function () {
    //Get book
});
$app->delete('/api/2.01/books/{id}', function () {
    //Create book
});
Community
  • 1
  • 1
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • What version of slim are you using? – nerdlyist Sep 22 '16 at 19:02
  • @nerdlyist I am using Version 3.4. – user1032531 Sep 22 '16 at 22:57
  • I know you accepted an answer but I am curious are you going to be creating a group then for every minor update and patch (assuming that is what the 2.x.x is) rather then just making the update in a v2 group. Seems like this will grow unnecessarily large. – nerdlyist Sep 23 '16 at 13:22

1 Answers1

1

If you are using Slim v2.0, you can do somtehing like:

// API group
$app->group('/api', function () use ($app) {

// Library group
$app->group('/library', function () use ($app) {

    // Get book with ID
    $app->get('/books/:id', function ($id) {

    });

    // Update book with ID
    $app->put('/books/:id', function ($id) {

    });

    // Delete book with ID
    $app->delete('/books/:id', function ($id) {

    });

});

as specified in the docs: http://docs.slimframework.com/routing/groups/

Pipe
  • 2,379
  • 2
  • 19
  • 33
  • Thanks Pipe. The documentation seems to be a little disjointed. I am using Slim v3.4. – user1032531 Sep 22 '16 at 22:58
  • Its similar... documentation for v3 can be found here http://www.slimframework.com/docs/objects/router.html#route-groups – Pipe Sep 22 '16 at 23:12
  • Thanks! I incorrectly assumed it would be set something like `$app = new \Slim\App(['base'=>'/api/library']);` – user1032531 Sep 23 '16 at 00:25