Dynamic Routing

Hey guys,

I’m new to slim and trying to make my way through. I’ve also tried so search for a solution but couldn’t find one. Consider the following:

$app->group('/v1', function()
{
    $this->group('/{controller}', function()
    {
        $this->get('', ['\API\Controllers\PersonController', 'index']);
        $this->post('',['\API\Controllers\PersonController', 'store']);
        $this->get('/{id}', ['\API\Controllers\PersonController', 'show']);
        $this->patch('/{id}', ['API\Controllers\PersonController', 'update']);
        $this->delete('/{id}', ['\API\Controllers\PersonController', 'destroy']);
    });
});

I need this pattern for multiple Controllers. But i don’t want to copy pasta it. So I’m searching for a solution to make the Controller call dynamic. I have already implemented a middleware which extracts the controller Argument and implements it within the request. But how, if anyway possible, can i get the param within the group so I can map the request to i.e. an array? Like:

['person' => '\API\Controllers\PersonController']

Or is there maybe a different way someone could give me a hint to?

Thanks in advance.

SEE:

1 Like

Thanks for your reply. For my usage your implementations seems a bit too much but it gave me enough hints to implement my own solution

Since you new to SLIM have a look at this tutorials. Well structured, you will gain more knowledge