How can I categorise routes in Slim Framework?
I have these basic/ front routes in my public dir,
$app->get('/about', function () use ($app, $log) {
echo "<h1>About ", $app->config('name'), "</h1>";
});
$app->get('/admin', function () use ($app, $log) {
echo 'admin area';
require dirname(__FILE__) . '/../src/Admin/index.php';
});
// To generic
$app->get('/', function () use ($app, $log) {
echo '<h1>Welcome to ', $app->config('name'), '</h1>';
});
// Important: run the app ;)
$app->run();
As you can see that when the route is on /admin, it will load the index.php in Admin directory.
Admin/index.php,
// API group
$app->group('/admin', function () use ($app) {
// Contact group
$app->group('/contact', function () use ($app) {
// Get contact with ID
$app->get('/contacts', function () {
echo 'list of contacts';
});
// Get contact with ID
$app->get('/contacts/:id', function ($id) {
echo 'get contact of ' . $id;
});
// Update contact with ID
$app->put('/contacts/:id', function ($id) {
echo 'update contact of ' . $id;
});
// Delete contact with ID
$app->delete('/contacts/:id', function ($id) {
echo 'delete contact of ' . $id;
});
});
// Article group
$app->group('/article', function () use ($app) {
// Get article with ID
$app->get('/articles', function () {
echo 'list of articles';
});
// Get article with ID
$app->get('/articles/:id', function ($id) {
echo 'get article of ' . $id;
});
// Update article with ID
$app->put('/articles/:id', function ($id) {
echo 'update article of ' . $id;
});
// Delete article with ID
$app->delete('/articles/:id', function ($id) {
echo 'delete contact of ' . $id;
});
});
});
Let's say I have article, contact, etc modules in my admin area. In each of these modules I have routes for get, put, delete. So I group them by module as in Admin/index.php. But I get 404 page not found, when I request these modules on my url, for instance http://{localhost}/admin/contact I should get list of articles on my browser but I get a 404.
I don't have this problem if I put the grouped routes in public/index.php but I don't want to clutter this index as in time I have more modules to add in. I prefer splitting the routes into different indexes.
So is it possible to split the grouped routes into different index.php in different locations (directories).
Or maybe this is not how I should do it in Slim? if so, what is Slim's way for solving this problem?