How do I effectively implement modules in an MVC framework, and handle routing to multiple Controllers in a single module?

StackOverflow https://stackoverflow.com/questions/12810899

Question

I've developed a basic MVC framework as a learning project in php--this is actually the second version of it, and I'm trying to improve two aspects that the first version fell short on:

  • Request Routing: mapping requests, e.g. /controller/action/[params]
  • Modules: drop-in applications designed to extend the application, e.g. a CMS.

This is where I'm at right now:

  1. I'm able to take a request and parse it into it's various pieces, e.g. controller, action, args, etc.. These map to corresponding controller classes/files, e.g. "/foo/bar" -> FooController::bar() - all of which is done in my RequestRouter class and encapsulated in a Request object.

    • I maintain a Manifest object that contains categorized references (controllers, lib, etc) to application files. The manifest is used by my autoloader method.
    • Since the manifest is cached, it is rebuilt whenever I add new files/classes, this true for when new modules get added/removed.
  2. The Controller::methods() render the correct views fine.

  3. Then comes the modules, which are organized like the core is structured (/root/raspberry/vendors/core/module)

The Problem

The issue that I think I'm having at the moment is a combination of Routing/Request Handling where modules are concerned:

  • If I request project.dev/admin it maps to the AdminController::index() -- this is correct
  • However, when I reference project.dev/admin/editor I still get AdminController::editor() where what I really want is EditorController::index()

After some research, I figure I could create a Decorator, which implements a Front Controller pattern and wraps a given Controller. The decorator could re-parse the request to make /editor the controller and re-map the remaining segments (/editor/action/args).

All of this seems like it could work fine, but I feel like I'm missing something fundamental earlier in the flow (RequestRouter). I've research other similar questions here in SO, and read up on HMVC, and in principle it seems like it might answer my questions, but it seems more interface-driven than framework-driven (if that makes sense?) I've also looked at other frameworks like Kohana, but I'm not quite grasping how their module system and routing to multiple controllers in the same module work.

Any insights or suggestions on how to effectively implement a module system without introducing a Front Controller or re-parsing a request would be much appreciated. Alternately, if I should re-structure my modules in a different manner, I'd like to understand how to do that.

Additional Info:

My RequestRouter maintains a list of routes that I pre-defined (including their default methods). Using these pre-defined routes, I can access /admin/editor and get EditorController::index(), but I'd have to define a route for every controller and request that goes to controllers in the module. I don't think this is good design. Here's a sample of my routes:

Array
(
    [/foo] => Array
        (
            [controller] => FooController
            [method] => bar
            [path] => /core
        )

    [/admin] => Array
        (
            [controller] => AdminController
            [method] => index
            [path] => /vendors/admin
        )

    [/admin/editor] => Array
        (
            [controller] => EditorController
            [method] => index
            [path] => /vendors/admin
        )

)

This is what my Request object looks like:

Request Object
(
    [properties:Request:private] => Array
        (
            [url] => /admin/editor
            [query] => 
            [uri] => /admin/editor
            [controller] => admin
            [action] => editor
            [args] => 
            [referrer] => Array
                (
                    [HTTP_REFERER] => 
                    [REMOTE_ADDR] => 127.0.0.1
                    [HTTP_VIA] => 
                    [HTTP_X_FORWARDED_FOR] => 
                )

            [get] => 
        )

    [request_status:Request:private] => 200
)

This is a sample of my Manifest:

[controller] => Array
    (
        [icontroller] => /htdocs/raspberry/raspberry/core/controller/icontroller.class.php
        [index] => /htdocs/raspberry/raspberry/core/controller/index.php
        [serviceerror] => /htdocs/raspberry/raspberry/core/controller/serviceerror.controller.php
        [admin] => /htdocs/raspberry/raspberry/vendors/core/admin/controller/admin.controller.ph
        [composer] => /htdocs/raspberry/raspberry/vendors/core/admin/controller/composer.controller.php
    )

This is the application file system:

http://s11.postimage.org/pujb2g9v7/Screen_shot_2012_10_09_at_8_45_27_PM.png

Was it helpful?

Solution

The issue seems to be caused by over-simplified routing mechanism. The impression i get is that you are using simple explode() to collect the parameters from URL. While this works just fin in basic examples, the setup will fail when you try to use a bit more advanced routing schemes.

Instead of splitting the string on /, you should match it against regexp patterns. Basically, you define a list of patterns to match against and first match is used to populate the Request instance.

In your situation would would have two patterns defined:

  • '#admin/(:?(:?/(?P<controller>[^/\.,;?\n]+))?/(?P<action>[^/\.,;?\n]+))?#'
  • '#(:?(:?/(?P<controller>[^/\.,;?\n]+))?/(?P<action>[^/\.,;?\n]+))?#'

If the first one fails, the second one would match.

P.S. You should know that controllers are not supposed to render output. Generation of response is responsibility of view instances. Views should be fully functional objects which contain presentation logic and can compose a response from multiple templates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top