Routing in micro framework

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();

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!

It varies between frameworks. Silex will allow you to specify a class/method pair to be lazy-loaded if the route matches:

Slim, on the other hand, suggests that you break up your route definitions across multiple files, so even though you’re not using classes, the controller logic is split up into more manageable pieces:

Many (Slim included) will allow you to pass a callable instead of an anonymous function, so you could pass an instance of a class and the method to call as an array (i.e. array($anObject, 'someMethod')) or the name of a static class method as a string 'MyClass::MyStaticMethod'.

Hi fretburner, thanks for your reply.

Yes, I’m aware of Silex that can call Controller and Action directly, but I’d like to talk about the others that only able to call callable function or instance of class (and I see many of them are doing this).

About calling an instance, how to do it, do we need to instantiate all of classes at first and then feed them to each route?

$blog = new Blog();
$post = new Post();
$profile = new Profile();
// and many other instances

$app->route('/blog', array($blog, 'index'));
$app->route('/post', array($post, 'index'));
$app->route('/profile', array($profile, 'index'));

It doesn’t make sense to me, because we need to instantiate all of classes even if they will be used or not on a request.

About Slim, they only separate the routing files as you mentioned, but in each file it’s the same (calling callable function). It’s true that it’s more manageable by separating the files but at the end it doesn’t make any difference as all files are being included in the index.php.

What I’m thinking is, a route should be the first filter that determines what our script will do. If a request is calling blog/index, then our script should run only blog controller and index action, it doesn’t need to do anything else like making an instance for blog post or user profile. Take it further, if we put the controller class in separate files, then we should be able to include only the controller file based on the route (maybe this one is too much for what a micro framework can do).

So, back to my question, what is the best way to call controller/action (or class/method) in the micro framework routes?

I agree, it doesn’t feel like a good solution to me either, although unless you’re talking about really big apps with a lot of controllers I suspect the performance impact is negligible.

If you combine anonymous functions with the ability to separate out groups of routes into separate files, you essentially get what you’re looking for - the anonymous functions won’t be executed unless the route matches, so there’s no concern about instantiating a load of classes that you don’t need. The downside would be that you wouldn’t be able to unit test your controllers.

Of course, there’s always the option to instantiate a controller class within the route callback, as you mentioned in your first post.

You might find these articles interesting - they both pertain to Slim, but you could apply the ideas to other micro-frameworks:

Again, thanks for your reply, fretburner.

I’ll take my time to read those articles you mentioned, thanks for finding those articles.