codeguy/Slim — pull request #461 by dhorrigan (2012-12-12 05:26:33+00:00)
Title: Allowing addition of Metadata to a Route.

This allows for a nice place to store some Metadata about a route which can then be used later in numerous different ways. The way I would like to use it is in our API. Different endpoint require different "kinds" of authentication. For example:

``` php
get('/test', function () use ($app) {
// Do Stuff
})->setMetadata([
'internal' => true,
'auth' => false,
]);
```

So here you see that this endpoint is an internal endpoint, thus requiring an Internal Only API key, but the request does not need to use any authentication.

I have this data then being read by a hook:

``` php
hook('slim.before.dispatch', function () use ($app)
{
$metadata = $app->router()->current()->getMetadata();
$metadata += [
'key' => true,
'internal' => false,
'auth' => true
];

// Secret DB/Header Sauce to get the Api-Key here

if ($metadata['key']) {
if (
(is_null($key) || $key->active != 1) ||
($params['internal'] && $key->is_internal != 1)
) {
$app->halt(401, 'Invalid Key');
}
}

// Auth Stuff Here
}, 1);
```

It is very useful and removes the need to write some convoluted code to make this sort of thing work.