Let's say I have a PHP Model-View-Controller framework that maps an address like http://site.com/admin/posts/edit/5
to an action that looks like
Posts_Controller::editAction($id)
in file /admin/controllers/posts.php
Now, many existing PHP frameworks that I've looked at would accomplish this by something similar to
$module = Router::getModule(); // "admin"
$controller = Router::getController(); // "posts"
$action = Router::getAction(); // "edit"
$params = Router::getParams(); // array(5)
$controller_file = "/".$module."/controllers/".$controller.".php";
$controller_classname = ucfirst($controller)."_Controller";
$method = $action."Action";
require_once $controller_file;
$controller_obj = new $controller_classname();
call_user_func_array(array($controller_obj,$method),$params);
To me, this smells bad and seems too "magical": I don't believe that you should be able to dynamically create classes based off of a string, then call methods on it specified as strings.
I have also seen alternatives that use reflection to call actions, which also smells bad.
Now imagine I have a larger, modular CMS built off of a similar MVC framework that has a table in the DB for each "page" type: blog, static, photo-album, etc... I imagine that the implementation for this is similar to my previous example.
Am I the only one who thinks this is bad? If not, shouldn't there be a design pattern that would cover this situation?
To Clarify:
Is there some good way to map a query containing information about module, controller, action, and parameters into
- A file to include
- A class to instantiate
- A method to call'
That uses as little "magic" (string-to-class or -to-method or reflection) as possible?
Even better: how would this be done in C# or Java, without reflection? I can allow for concatenating strings to arrive at a file location.