How can I create group routing in PHP with Closure? I'm creating my own REST API from scratch in PHP for practice and learning.
In bootstrap file i call App class:
$app = new App/App();
$app->import('routes.php');
I have routes.php file with:
$app->group('/api/v1', function() use ($app)
{
$app->group('/users', function() use ($app)
{
$app->get('/', 'User', 'index');
$app->post('/', 'User', 'post');
$app->get('/{id}', 'User', 'get');
$app->put('/{id}', 'User', 'put');
$app->delete('/{id}', 'User', 'delete');
});
});
It needs to create routes like this:
- /api/v1/users/
- /api/v1/users/
- /api/v1/users/{id}
- /api/v1/users/{id}
- /api/v1/users/{id}
App class:
class App
{
public function group($link, Closure $closure)
{
$closure();
}
}
And it sets routes like this:
- /
- /
- /{id}
- /{id}
- /{id}
What should I do to prefix urls ? How can I "foreach" these other $app->get(), $app->post() method callings ?