Multiple routing files (per directory)


#1

Hi,

How do I do to use a modular approach to route files like this project ? https://github.com/userfrosting/UserFrosting

Per module routing in other words.

Have a nice day!


#2

I’ve written a RouteResolver that auto-routes to Controllers (and functions within that controller)
It’s a multi-module setup and per module multiple controllers.
You can always code the routes by hand (that was also suggested to me, but hey: call me lazy)


#3

I had created a folder structure like this:

<Project Root>
- config
- modules
-- UserFrosting
--- controllers
--- views
--- models
-- OtherModule
- static

Then with a single router, I catch the module name, the controller name, and other possible data like this:

$app->group('/:module/:controller', function () use ($app) {
$app->get('/', function ($module, $controller) use ($app) {

   // Utils class is custom made to parse modules and classes names
    $md_name = Utils::Slug2CamelCase($module);
    $ctlr_name = Utils::Slug2CamelCase($controller);

    $class = new ReflectionClass($md_name . '\' . $ctlr_name . 'Controller');
    $instance = $class->newInstanceArgs(array($app));

    $response = $instance->index();
    $app->response()->write($response);
});

// Do the same with other verbs/methos
$app->get('/:id', function ($controller, $id) use ($app) {});
$app->post('/', function ($controller) use ($app) {});
$app->put('/:id', function ($controller, $id) use ($app) {});
$app->delete('/:id', function ($controller, $id) use ($app) {});
});

Note: I am using reflection class to instance and call