I'm building a MVC where everything is a module. This will allow me to separate collections of objects like forum and user modules to further organize my MVC.
/blog
/controller
/model
/view
/user
/controller
/model
/view
/forum
/controller
/model
/view
I currently map routes for something like forum/edit/34
to /forum/controller/edit.php
which contains the Forum_Controller_Edit
class. (This is slightly different from the standard approach where all actions are methods of a controller Forum_Controller::edit()
). This allows me to separate all the individual "actions" of a controller into their own files so I don't have to manage large controllers with 10 different actions.
Anyway, here is the problem. Suppose I want to add new controllers to a module. For example, lets say I want to have forum topics (forum/topic/view/34
). I could create a new "topic" module.
/forum
/controller
/model
/view
/topic
/controller
/model
/view
But then 1) I would have to use URL like /topic/view/
instead of forum/topic/view
, 2) I would have separated related components (topic should be a part of the forum module), and 3) I would now not be able to create a general "topic" module for something like content nodes if I wanted to.
So how can I build a modular MVC while allowing multiple controllers (with multiple actions) in each module?
Kohana Framework decided to implement a HMVC to solve this problem. However, that results in many is_file() checks to find the correct location of the file you wish to load. I/O checks are very precious on commodity hardware so I don't consider that an option. Also, it is not apparent which version of a class is being loaded which can lead to confusion when working with multiple classes with the same name.
I would like to find a solution to this problem that does not involve pre-setting paths or checking folders for the right class. Yet, of the last 20 frameworks I looked through they don't seem to have a good answer to this universal problem. Anyone have some ideas?
If I was to implement routing, how would it work? How would I handle first (/forum), second (/forum/view), and third levels (/forum/topic/view)? How would my routes know the difference between a third level route and a second level route with a parameter? In other words, which of these would my system try to do? (Given the URI "forum/topic/view/34")
$controller = new \Forum\Controller\Topic\View;
$controller->action('34');
// or
$controller = new \Forum\Controller\Topic;
$controller->view('34');
// or
$controller = new \Forum\Controller;
$controller->topic('view', '34')
Taking Kyles advice I decided that ALL controllers will need a route set which will allow me to define which of the classes above the URL path "forum/topic/view/34" routes too. After all, EVERY URL is important and should not be based on the filesystem structure you have in place. I believe he is right in saying that the URL design deserves more attention than we often give it.
I would like to find a solution to this problem that does not involve pre-setting paths
It's called routing, and that is the solution.
of the last 20 frameworks I looked through they don't seem to have a good answer to this universal problem
The universal solution is routing.
That is precisely why frameworks employ it. Routing is the way to retain your architecture while still having flexibility with your URIs. Otherwise, your URI naming would always have to conform to your MVC layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With