This might be a bit hard to comprehend so I apologize in advance if this is not clear enough.
I’m writing my own MVC framework and am once again stuck.
I am in the process of writing the controller classes for the framework. Basically this is how it works:
- Instantiate class coreController which extends abstract class
- coreController sets controller to be loaded by interpreting query string
- query string values stored in variables
- other variables assigned values
- new controller is loaded
- new controller checks if an action object needs to be instantiated.
- new actioncontroller is loaded
- action controller checks if it is the final object required.
- action controller is returned as an object to be referenced during the rest of the script.
generic $controller->method() can be called and references final controller loaded.
Another overview:
coreController
pageController
pageControllerActionAdd
return as object to start
$controller->something(); //References pageControllerActionAdd
Esentially what I want to be able to do is be able enter a url like:
http://www.mywebsite.com/page/modify/
and have the script pull up the PageModifyController as a variable so I can execute it’s methods.
If you can tell me a better method for what I am doing please go ahead. You don’t have to write any code, just the idea would be great. It is just that the way I am currently doing is very confusing and hard to debug. I will end up with multiple nested objects and I don’t like the concept of that.
I’ve been reading a lot of other source code and found that it too can be quite sophisticated.
I actually created a framework that works along the lines you are trying to implement. I think what you are missing is a RoutingHandler class. Routing is the physical manipulation of the URL, which tells your application which Controller to load, and which Action to run.
In my world I also have Modules, so the basic routing scheme is
These three items map to my URI scheme in that fashion. Variables can be appended also like so…
http://www.domain.com/module/controller/action/var1/val1/var2/val2
So, what happens after the URI is parsed, and control is passed over to the appropriate controller and action? Let’s make some code up to demonstrate a simple example…
In the
Initializemethod, I’m setting some controller-wide stuff, and grabbing an instance of my Model to use later. The real meat is in theindexActionmethod. This is where you would set up stuff to use in your View. For example…_CONTROLis an array of values that I manipulate and pass onto the View. TheControllerclass knows how to find the right template for the View because it is named after the Action (and in a sibling directory).The
Controllerparent class takes the name of the action method and parses it like so…You can also do some other fun stuff here, for example…
…would tell the Controller not to render inside a template, but just complete the method. This is useful for those situations where you don’t actually want to output anything.
Lastly, all of the controllers, models, and views live inside their own Module directory like so…
I can have as many Modules as I need, which means I can separate functionality out by Module and/or Controller.
I could go on, but I’m writing this from memory, and there are some wrinkles here and there, but hopefully this gives you food for thought.