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

前端 未结 1 1066
花落未央
花落未央 2020-12-21 04:18

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

相关标签:
1条回答
  • 2020-12-21 05:07

    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.

    0 讨论(0)
提交回复
热议问题