1

I'm trying to accomplish API versioning of the APIs I've written using Slim framework.

All my versioned APIs look like this:

$app->get('/:version/book/search', function() {...});

I'm trying to create an application wide Route Condition for this version as follows:

\Slim\Route::setDefaultConditions(array(
    'version' => 'v[3-6]'
));

So only the APIs with version number v3,v4,v5 and v6 should be allowed to get it.

My requirement is to store the exact version of the API call made in $app->version, and then do version specific code changes if needed for that. I have created a middleware which I added to the $app itself, so it gets executed for each API calls:

$app->add(new \GetVerMiddleware());

class GetVerMiddleware extends \Slim\Middleware
{
    public function call()
    {

        // HOW TO GET THE version route parameter??
        // ????
        $app->version = $version;
        $this->next->call();

    }
}

So I want to know how to get the route parameter version inside the GetVerMiddleware. Is it even possible to get that? I know how to get the entire route printed (link), but I'm interested only in the version parameter.

Community
  • 1
  • 1
Ouroboros
  • 1,432
  • 1
  • 19
  • 41

1 Answers1

0

OK, I've figured out the solution after some researching, the following link particularly helped:

Slim Framework forum

$app->add(new \GetVerMiddleware());

class GetVerMiddleware extends \Slim\Middleware
{
    public function call()
    {

        $this->app->hook('slim.before.dispatch', array($this, 'onBeforeDispatch'));
        $this->next->call();

    }

    public function onBeforeDispatch()
    {
        $route_params = $this->app->router()->getCurrentRoute()->getParam('version');
        $this->app->version = $version;
    }
}

I think the solution was pretty much there, apologies!

Ouroboros
  • 1,432
  • 1
  • 19
  • 41