SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb MVC Model Appropriate?

    I am wondering if MVC model is appropriate for this type of action.

    Firstly, I am programming in PHP4 so only use examples that are compatitable and work in PHP4.

    I was reading up on the MVC model and I am wondering if the use of FrontController with InceptingFilters, PageController would be appropriate for an BMS (Blog Management System) that creates content dynamically via calls from the script to MySQL via the HTTP Request.

    For example, I type in http://www.domain.com/index.php , the application should run so that it selects the appropriate action, in this case its a default action, so it grabs data from mysql and then appends it to the appropriate template data also obtained from mysql.

    I am not using any template files, so no (index.tpl) files or the like, just gets template data from MySQL via a database fetch.




    Is this viable?
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's viable of course, have you looked at the sticky MVC thread on this forum yet? This basically will cover just about everything you need to know about MVC in my view, and if not you can ask

    I'm pondering on why your using the database to hold templates though? There are few occasions you would need to store them in a database, and I feel for a blog system flat files would serve you better... such as when you have to cache them?

    Yet to see anyone (attempt even) a database based template cache

  3. #3
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    I'm pondering on why your using the database to hold templates though?
    We've already had a thread about that, http://www.sitepoint.com/forums/show...light=template

    He wants to use his DB for templates, so let him use his DB for templates!

    Attached is my view of it

    Is your site going to be interactive in any way? If so, do you have these little flow charts for adding data to the database?

    I'd reccomend a 302 redirect or something after you've added data to the db, so you could have:

    POST to my.com/add -> 302 Redirect to my.com/list
    GET my.com/list -> 200 HTML OK

    (You could do the redirect internally if you want, it depends how much programing you want to do!)

    That second line would not know that there was just a post, it would just do what it would do any other time. That's how Ruby on Rails works, and it works great there. (You just do "redirect :action_name" and it handles redirection etc for you, nice )

    Douglas
    Attached Images Attached Images
    Hello World

  4. #4
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it helps, MVC just makes a point about separating controller and view in the traditional layering: presentation - domain - data source (View and Controller are both part of the presentation layer).

    PS: Redirect smells awfully like goto...

  5. #5
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well my idea is that I want to be able to call the files like this:

    http://www.domain.com/index.php?a=search - pulls up a search page
    http://www.domain.com/index.php?page=privacy - pulls up a page called privacy
    http://www.domain.com/index.php?cat=blah&page=moo - pulls up a page called moo in a category called blah.

    This way if I want to used mod_rewrite I can create SEF Urls in a simple matter to

    http://www.domain.com/search/
    http://www.domain.com/privacy/
    http://www.domain.com/blah/moo/

    See what I am getting at?
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doug,

    The diagram you posted has the View gather the template, or know something about the database? I'd be more inclined to think that the Model (via Controller) would know which template to use (since in this case, the template is database based) to then pass to the View, rather than the View it's self know about the template source

    This I can gather from your diagram only and if I've missed something... ??

  7. #7
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have an example of a FrontController and/or PageController in PHP4 Syntax?
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WACT is all PHP4. The crud examples show nested controllers in a composite pattern.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, is it appropriate to my DAO requirement? I believe that app uses flatfiles, which I don't want to use.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  10. #10
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone know me a DAO that effectively using the FrontController with PageController with InterceptingFilter?

    I would be much obliged.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  11. #11
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bear in mind that a DAO exists in a different layer of your application to your Front Controller and the other patterns you mention - those are presentation tier patterns. Your data access and storage will be entirely independent of them, and certainly won't "use" them.

    Secondly, you won't use Front Controller with Page Controller - they are different patterns entirely. I think you are looking for Front Controller + Command.

    Maybe have a quick look at these two (PHP4):
    http://www.objectmonkey.com/index.ph...=1^indexid=11^
    http://www.objectmonkey.com/index.ph...Eindexid=11%5E

    ...

    Fwiw, I'm very much with the 'redirect after successful action' school too - I've seen far too many duplicate records in tables, or duplicate emails sent in my time not to. Not to mention the way cleaner code it leads too.

  12. #12
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pointbeing
    Secondly, you won't use Front Controller with Page Controller - they are different patterns entirely. I think you are looking for Front Controller + Command.
    "Commands" are actually very similar to PageControllers except that they aren't the first point of contact with the http request.

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slightly Off Topic but I too am seeing the benifits of a redirect on a POST submission.

    McGruff,

    "Commands" are actually very similar to PageControllers except that they aren't the first point of contact with the http request.
    I'm sure you've made this clear as to why though I have a bad memory... Care to explain again?

  14. #14
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Secondly, you won't use Front Controller with Page Controller - they are different patterns entirely.
    Yes and no. You can implement them separately or have a PageController or ApplicationController behind a FrontController. Fowler points out the confusion with the term Controller and lumps them all into a category of "input controllers."
    Christopher

  15. #15
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    PS: Redirect smells awfully like goto...
    Not really, it is more like treating your entire application as a function, and the paramaters that you pass make up the URL you use. This is really what happens all the time when your users click links to go from one page to another, using a redirect is much the same as getting your application to click links for you. The problem with goto is that it hides flow control, using the redirect method starts your application off at the top again, so doing two things (adding the POST and displaying a list of results) gets split up into two requests.

    The diagram you posted has the View gather the template, or know something about the database? I'd be more inclined to think that the Model (via Controller) would know which template to use (since in this case, the template is database based) to then pass to the View, rather than the View it's self know about the template source
    My View layer always knows where it gets its templates from, the model just says which template it wants and the View layer handles getting the template. Just having the templates in the DB instead of files shouldn't change anything outside the View layer, that's the whole point of having a View layer! It lets you change how the view is generated without changing your model.

    Naturally the View can usethe same DB abstraction layer as anything else that uses the DB, you don't need to use two file systems to store PHP files and Gif files, do you? Though different folders (= different DB tables here) are always helpful!

    Douglas
    Hello World

  16. #16
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you will forgive the ancient article there is a diagram which talks about redirection. Including:

    Quote Originally Posted by old article
    Tip - redirection:
    Whenever a user enters a mode that causes a database statement like an update or a delete statement to execute, always redirect the user back to the application with a mode that will display a view. This is done in case the user then hits the refresh button. Without the redirect, the script might attempt to re-run the SQL script, generally with undesired results.
    Replace the "mode switch" with front/application/page/action controller, and view switch with a show action view and a view factory and similar logic still applies for the benefits of a redirect.

  17. #17
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From a purely theoretical viewpoint, I can't see any difference between goto and redirects, and so redirects also must be "evil". They distribute ApplicationController logic which could have been kept in a single location.

    Practically, I do accept it's a valid solution - possibly the only solution if you weren't maintaining any state between requests. With sessions, another option would be to store randomly generated form id's and compare these if another submission of the same type is received from the user.

  18. #18
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    They distribute ApplicationController logic which could have been kept in a single location.
    Can you expand on this? I don't understand what you mean by distributing the logic, there is still only one controller.

    Douglas
    Hello World

  19. #19
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    From a purely theoretical viewpoint, I can't see any difference between goto and redirects
    And I can't see how they could be considered the same; parhaps you could demonstrate the similarity? And possibly you could use the same example to demonstate that a function call is not the same a goto?

    Thanks,
    Douglas
    Hello World

  20. #20
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian R. Gordon
    Is this viable?
    Try it out and get back to us.
    Hello World

  21. #21
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Can you expand on this? I don't understand what you mean by distributing the logic, there is still only one controller.
    My mistake: a simple 200 page doesn't have an ApplicationController since it doesn't have any page choices to make. Still, a redirect seems to suggest an inadequacy of some kind. A script ought to be able to handle whatever is thrown at it.

  22. #22
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    A script ought to be able to handle whatever is thrown at it.
    Yep, I agree with that. You've probably got a broader definition of "handle" than me though. Say I have a script which is going to, for example, record a blog post made by a user. That's all I'd want it to do: take data from POST, and then put it in the database. If there is a problem with the data, it should have a mechanism to handle that (redisplay the form with the POST data with error messages etc) but that's where its responsibilities end. I'm going to have another script to display the list of the latest blog posts, so for me the cleanest way to get there is to redirect. I'm guessing you would have both parts, call them the "application", together with some controller code to run one after the other, right?

    Douglas
    Hello World

  23. #23
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I appreciate all the help.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  24. #24
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just so we are clear this is what I am hoping to use the MVC pattern to accomplish. I don't know if I really need to use this pattern or if I would be better served just coming up with my own method but, I am just looking for something that is effective in terms of developing the application to easily accomplish this line of actions.






    1. User requests index.php?page=about
    2. Conditional
      a. If "page" is a listed action, then execute to next step
      b. If "page" is not a listed action, show error
    3. Two Part MySQL Query
      a. Get "about" data from the correct content table
      b. Get "page" template data from template table
    4. Combine the data fetched from the database into the correct variable structure to have complete HTML page.
    5. Show resulting HTML page to user


    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  25. #25
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian R. Gordon
    Just so we are clear this is what I am hoping to use the MVC pattern to accomplish.
    Can you show us which parts of the code you've written and possibly the bits you think you might have an MVC problem with?

    Douglas
    Hello World


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •