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.
Currently I have:
Also the app folder contains only static information at the moment, am I doing that right? Doesn't feel right...
-other stuff like filehoster save locations
Thanks for your help in advance, kind regards
Last edited by NogDog; 07-07-2014 at 10:10 PM.
Reason: added [code] tags for formatting
I might organize it something like:
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).
config (DB settings, routes, etc.)
static (non-dynamic files)
utilities (function definitions, etc. that don't fit elsewhere
libraries (any 3rd-party code you want to use that doesn't fit elsewhere)
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).
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".
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?
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?
Originally Posted by HES_Xenon
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.
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.
Originally Posted by HES_Xenon
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.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread