Jump to content

old_iron

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

old_iron's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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..
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.