Slim Framework, Route Groups, and Parameters

Yesterday, I was writing a new sub-endpoint for an API based in Slim Framework. For this API, the routes are kept in separate files for organizational purposes, but also because route groups didn't exist at the time; so I thought I'd give them a shot.

My initial version of the code looked like the following, as I'd (incorrectly) assumed that the group closure took the parameter.

 
<?php
 
$app->group('/:school_id', function ($school_id) use ($app, $ioc) {
    $app->get('/applications', function() use ($app, $ioc, $school_id) {
        return $this->get($school_id);
    })->name('applications:all');
 
    $app->get('/applications/:application_id/', function($application_id) use ($app, $ioc, $school_id) {
        return $this->get($school_id);
    })->name('applications:id');
});
 

However, the code should actually look like the following, because what's essentially happening is that $app->get() realizes it's in a group, and adjusts the route accordingly, causing the parameters to be passed there.

 
<?php
 
$app->group('/:school_id', function () use ($app, $ioc) {
    $app->get('/applications', function($school_id) use ($app, $ioc) {
        return $this->get($school_id);
    })->name('applications:all');
 
    $app->get('/applications/:application_id/', function($school_id, $application_id) use ($app, $ioc) {
        return $this->get($school_id);
    })->name('applications:id');
});