On 07/12/2012 10:21 PM, Simon Dániel wrote:
I have started to develop a simple MVC framework.
I have a base controller class which is abstract and all of the
controllers are inherited from that. Every controller contains actions
represented by methods. (E. g. there is a controller for managing
product items in a webshop, and there are seperate actions for create,
modify, remove, etc.) There is also a default action (usually an index
page), which is used when nothing is requested.
But what is the best way to invoke an action? I can't do it with the
baseController constructor, becouse parent class can't see inherited
classes. And I can't do it with the constructor of the inherited
class, becouse this way I would overwrite the parent constructor. And
as far as I know, it is not a good practice to call a method outside
of the class, becouse the concept of operation of the class should be
hidden from the other parts of the application.
By reading your description it sounds like you are using a "C" pattern
rather than the "MVC" pattern.
For example you are saying that "there is a controller for managing
product items in a webshop". This should be the job of a Model, not a
MVC is generally the following:
Model is responsible for data.
View is responsible for presentation of data.
Controller is responsible for connecting Model and View.
In web applications there is also usually some kind of URL Router in
front of the Controller.
For example when the framework receives a request to "/items/get_all",
the get_all method in ItemsController is executed.
get_all will proceed by loading an ItemModel and executing getAll on it,
which returns an array of items.
The controller now loads a new View and passes the array of items to it,
which is then rendered as HTML and send to the browser, or whatever the
View wants to do.
(If the Model didn't return what we expected, the Controller would load
another View that simply contains "I'm a teapot!" or "No items available
in this category!")
This is great, because now we can have multiple Controllers accessing
the same data without having to inherit or duplicate code (DRY).
The Controllers also doesn't care if the data is stored in MySQL,
Postgres, in RAM or on butterflies or anything else, as long as data is
returned in an expected format.
The View can of course be changed to another to easily change the
appearance (representation) of a resource.
I'd suggest reading some more about MVC before going any further, and
other design patterns wouldn't harm either :-)
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php