MVC practices
Posted: Mon Dec 03, 2007 7:04 am
Ok this is not quite the typical question about MVC. I have made my own framework that uses MVC and runs perfectly fine. Due to silly bosses being influnced by even sillier Finns I have to chance frameworks to something that is more commercial "just because".
I chose Zend Framework because I want choice. I want to be able to do things the best way, not what I am told is the best way. Insert other arguments here.
Now the problem is that with my framework (NFW), I did things like this:
page: news.php
view: news.view.class.php
controller: news.controller.class.php
model: news.model.class.php
When you went to news.php, it would check if you have an "action" set or a "view" set. Actions are specifically reserved for things like updating the database, inserting new rows, deleting data, the kind of stuff that changes data. All actions are in the controller class so if you did news.php?_action=update&title=new_title&id=35 then you would hit the "update" method in the controller class and update the title to "new_title" where the id is 35. Then, if no view is specified, you are sent to the main() method of the news.view.class.php class (if you supplied a view, you are sent to that method).
In the view class I assign variables and everything that is going to be in the template. So I will set the page title, grab a list of the news (depending on the method i am in) and set all these variables to my templating class which is actually just PHPtal. Then PHPtal itself opens up the correct template file and throws the variables at it and everyones happy.
The model class just stores the table name and the table fields (and maybe what is the primary key and whatnot). Nothing big. I do use the model to execute all my queries though as every model is linked to my database class.
Fast-forward to me trying out Zend Framework!
It seams like they want to combine what I do in the view and in the controller all into the controller class. Then the "view" is only the template. This seams silly to me because I am used to separating my code to make it easier to maintain. If I want to change what happens when you click "submit", I change the controller. If I want to change what happens when you click on the news headline to read the whole article, I edit the view (and possibly the template).
Now, when I go to import everything over to the Zend Framework, I already have views that are about a thousand lines long and controllers that are the same. Does it make sense to combine my view and controllers together into 1 controller and just do it or am I missing something here? Should I move what I have currently in the view to the controller, and then what I have in the controller to the model? What would make sense here?
I chose Zend Framework because I want choice. I want to be able to do things the best way, not what I am told is the best way. Insert other arguments here.
Now the problem is that with my framework (NFW), I did things like this:
page: news.php
view: news.view.class.php
controller: news.controller.class.php
model: news.model.class.php
When you went to news.php, it would check if you have an "action" set or a "view" set. Actions are specifically reserved for things like updating the database, inserting new rows, deleting data, the kind of stuff that changes data. All actions are in the controller class so if you did news.php?_action=update&title=new_title&id=35 then you would hit the "update" method in the controller class and update the title to "new_title" where the id is 35. Then, if no view is specified, you are sent to the main() method of the news.view.class.php class (if you supplied a view, you are sent to that method).
In the view class I assign variables and everything that is going to be in the template. So I will set the page title, grab a list of the news (depending on the method i am in) and set all these variables to my templating class which is actually just PHPtal. Then PHPtal itself opens up the correct template file and throws the variables at it and everyones happy.
The model class just stores the table name and the table fields (and maybe what is the primary key and whatnot). Nothing big. I do use the model to execute all my queries though as every model is linked to my database class.
Fast-forward to me trying out Zend Framework!
It seams like they want to combine what I do in the view and in the controller all into the controller class. Then the "view" is only the template. This seams silly to me because I am used to separating my code to make it easier to maintain. If I want to change what happens when you click "submit", I change the controller. If I want to change what happens when you click on the news headline to read the whole article, I edit the view (and possibly the template).
Now, when I go to import everything over to the Zend Framework, I already have views that are about a thousand lines long and controllers that are the same. Does it make sense to combine my view and controllers together into 1 controller and just do it or am I missing something here? Should I move what I have currently in the view to the controller, and then what I have in the controller to the model? What would make sense here?