Hi guys,
I’m learning some micro frameworks but I’m confused with how they explain how the router works. I’m not talking about specific framework, most of them are giving similar example about routing like this:
$app->route('/hello', function() {
echo 'hello world';
});
It looks easy if we want to call a few tasks inside the function, but usually we need to run more than a few process, so we need to put it in a class/controller. So, how to call a controller class and action method, do we need to instantiate the controller class in the anonymous function? Like this:
$app->route('/blog/index', function() {
$controller = new Blog();
$controller->index();
});
Is there a better way to do that? because I think the anonymous function is a bit waste there.
Also, I usually put the classes in separate files, do we need to include all these files before evaluating the routes? actually I’d like to ask further about lazy loading technique, but let’s discuss about this first. Thank you!