www.webdeveloper.com
Results 1 to 9 of 9

Thread: MVC - Standard methods

  1. #1
    Join Date
    Jul 2014
    Posts
    4

    MVC - Standard methods

    Hi, first of all i hope this is not a duplicate thread, if so please point me in the right direction i couldn't find anything related.

    The issue is that I'm rather new to web development and I thought it might be a good idea to use the mvc architecture (no framework, i want to know how things work from scratch). Now I think I have understood the concept (as explained in sitepoint tutorial, maybe somebody can shed some light on the different "versions" of mvc?) but i don't know where I should put standard methods like converting an array to a string, mysql methods of all kind and so on. They don't really seem to fit in the model because the contain markup sometimes, but also not in the view because sometimes they fetch or manipulate data.

    Also, I'd like to know where to put JavaScript or AJAX files in the structure?
    Currently I have:
    Code:
    /
     -app
     -mvc
      -controllers
      -models
      -views
     -templates
     -other stuff like filehoster save locations
     -index.php
    Also the app folder contains only static information at the moment, am I doing that right? Doesn't feel right...

    Thanks for your help in advance, kind regards
    Xenon
    Last edited by NogDog; 07-07-2014 at 11:10 PM. Reason: added [code] tags for formatting

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    I might organize it something like:
    Code:
    app
      config (DB settings, routes, etc.)
      controllers
      models
      views
      static (non-dynamic files)
        javascript
        css
      utilities (function definitions, etc. that don't fit elsewhere
      libraries (any 3rd-party code you want to use that doesn't fit elsewhere)
      index.php
      .htaccess
    Not sure why you need a "templates" directory -- seems like part of views (which can be hierarchical and have their own inheritance scheme of sorts).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    For DB stuff, I would start by using PDO as your DB extension, so that you're not tied down to a specific DBMS. One of your config files would contain the DSN string used as the first parameter to PDO::__construct(). Your base Model class could have a $pdo property that gets populated by the constructor (perhaps a boolean argument if you think you might have any models that don't need DB access?), possibly from a singleton child class of PDO (so that you always have a single connection in case you ever load 2 models at once for a given transaction).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    And, of course, if we wait around long enough, DeathShadow will stop by and tell us why you should not bother with MVC at all (with plenty of "colorful" language). As far as I'm concerned, I don't care if he's right or wrong: if you're going to work in this area of web development, you'd best have a good understanding of MVC whether you like it or not, just as you should know how to work with JQuery whether you like it or not, etc. -- unless you plan on always being a one-person web dev "team".
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2014
    Posts
    4
    Hi NogDog, thanks a lot this looks like a very usable structure, I'm on it
    What exactly is the semantic difference between utilities and static? And what is PDO? Could you give me a link to a good source on that?
    I'll be honest, MVC is the first architecture I came across that looked useful, are there any recommendations on which architecture to use?

  6. #6
    Join Date
    Jul 2014
    Posts
    4
    Oh, and another question: In order to construct the view, model, controller objects i'd have to include the files in the index.php (obviously). So, how do I control which views, models and controllers to use? A switch at the beginning? Also, would I make one file for each view, or write many classes in one file? Any potential problems doing that?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    Quote Originally Posted by HES_Xenon View Post
    Hi NogDog, thanks a lot this looks like a very usable structure, I'm on it
    What exactly is the semantic difference between utilities and static? And what is PDO? Could you give me a link to a good source on that?
    I'll be honest, MVC is the first architecture I came across that looked useful, are there any recommendations on which architecture to use?
    For me, "static" would be for files that have no server-side processing. They just get requested by the rendered HTML pages as/where needed (JavaScript files, CSS files, image files, etc.).

    See http://php.net/pdo for more on PDO. It's a PHP extension that generalizes your DB interface so that it's not specific to MySQL, PostgreSQL, etc. It includes the ability to create prepared statements with bound parameters, which is a good way prevent SQL injection.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    Quote Originally Posted by HES_Xenon View Post
    Oh, and another question: In order to construct the view, model, controller objects i'd have to include the files in the index.php (obviously). So, how do I control which views, models and controllers to use? A switch at the beginning? Also, would I make one file for each view, or write many classes in one file? Any potential problems doing that?
    I would take the one class per file approach, and make use of spl_autoload_register() as much as possible in order to avoid lots of require_once() calls.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jul 2014
    Posts
    4
    Thank you very much for your help! I just wanted to like this... a.) is there a similar function here? b.) am I spending too much time on facebook? c.) b is rhetorical.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles