old_iron Posted August 10, 2008 Share Posted August 10, 2008 Hello ladies and gentlemen, I'd like to get some input on how I can best improve my web application programming. During college I played around with writing a framework for myself, and now I've improved it and use it for web applications. It works fairly well at the moment but I know it could be better. I use my interpretation of the MVC pattern, and use Controllers to populate variable elements of a web page. Each controller calls an Action (a sub-Controller) and this action returns a view, for example an html table of articles fetched from a database. I use a Data Access Object to get the data required. Actions may consist of further sub-Actions so if a certain page element has two sections, each would be created from a sub-Action. Here is some sample code: (not the exact code but close enough) <?php //this class controls the entire web page class FrontController { function run() { //parse request $r = new Request(); //instantiate appropriate action $a = new Action($r); return $a->run(); //or call multiple Actions for multiple page sections } } class Action extends Controller { function __construct(Request $r) { $this->r = $r; } function run() { return $this->execute(); } function execute() { $data = DAO::getData($param); //get some data from the database //do something with $data maybe return $data->render(); //or call multiple sub-Actions and return the complete composite view from these actions } } ?> I can see some things wrong with my code already. First, I am using the Controller to directly access the data layer, and also putting business logic in Controller classes (Actions are still Controllers). As I understand it, manipulation of domain objects/data should be done by a model class and not by a controller class. 1. Have I got this right? Secondly, right now for complex views with multiple variable sections, I am using a seperate class for each action. So if I had an 'articles' module, then the base Action would be ArticlesAction which would parse the request and find that, say, the user wants to see a list of all articles. So this ArticlesAction would call a ShowArticlesAction which would return the rendered data. ArticlesAction may call more than one action, such as load a list of the Top 5 Viewed Articles and show this on every articles page regardless of whether the user is viewing one article, a list of articles, an article search page, whatever. So it might load a ShowArticlesAction and a ShowTopFiveArticles action. This requires creating module-specific actions (classes) for every action, which may be as trivial as getting a simple article count from the database and displaying it on every article page. 2. Would I be better served with a 'model' class with methods instead of seperate classes for each action? So each 'action' could be a method in the class. Say I'd use an ArticleModel which could be given parameters (which method to run and its parameters) and return an overall, possibly composite, view. This way all module-specific operations would be put in one class and avoid me having to create classes for trivial actions (I am sure running a method in an instantiated class is better performance-wise than instantiating a new class and running one of its methods). 3. Most website operations include fetching or saving information from/to a database or other data source. The way I have things now, I create a class corresponding to an entity created from a database (not necessarily one class per table) and then create Action classes to manipulate those objects, or if 2. is approved, I may add a method to a Model class. I'm not even sure if this is the best way to go about things, the simplest would be to create an Article object and include all the operations in there without the need for a Model class at all.. has anyone done this? Any ideas would be gratefully appreciated, this is a learning curve for me done for the experience as well as the challenge of programming efficiently.. Quote Link to comment Share on other sites More sharing options...
quantumgroup Posted September 10, 2008 Share Posted September 10, 2008 I'm actually in the process of creating my own application framework right now, so I get where you're coming from with most of these questions. 1. Scratch it. 2. Go with it, much simpler, much easier. If you need to hide methods from the outside make them private/protected. Usually I also name them starting with an underscore for further differentiation. 3. The Article class would BE the model class. It's perfectly fine to do it that way. Quote Link to comment Share on other sites More sharing options...
xylex Posted October 17, 2008 Share Posted October 17, 2008 I'd agree with Quantum on the scrap and scale back. What you're proposing sounds like a pretty procedural setup. As your web app expands, your modules are going to overlap a lot so you'll want to be able to let the components interact in the same manner as the main caller can use them. As for what goes in which layer, I think the key is consistency and abstraction, rather than trying to follow a design pattern to a T. As long as all data access is done at one layer, all business logic is done at one layer, etc, it'll be maintainable. The test I like to use is "If I cut the layer here, put it on another server and rewrote it in Java, would the business flow still make sense, and would everyone understand what PHP would do and what Java would do?" Quote Link to comment Share on other sites More sharing options...
Acs Posted October 17, 2008 Share Posted October 17, 2008 When you have the code the way you want it, start using xdebug to see bottlenecks etc Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted October 18, 2008 Share Posted October 18, 2008 IF this was your first framework then chance are there are going to be a lot of thing you are going to improve on in speed(I am in the process of rewrite my first framework which currently work pretty well). The biggest thing i found is that I added a lot of code in the core system that was not really needed and just created bottlenecks for myself where i could have had a little bit of speed improvement so XDebugging code is a great way to improve on the efficiency of your code. Quote Link to comment Share on other sites More sharing options...
Acs Posted October 18, 2008 Share Posted October 18, 2008 Also if you are on windows don't forget to use wincachegrind to read the cachegrind files generated by xdebug they are tremendously helpful!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.