aCarella aCarella - 6 months ago 13
PHP Question

MVC Architecture from Development to Production

I am trying to gain a better understanding of MVC architecture. I write both front end and back end web applications using LAMP stack. I don't use any frameworks yet. I code in Notepad++ and use WinSCP for file transfer (Putty for terminal). All of the server side coding is done in PHP.

Based on the reading I've done, this is my best guess on how MVC's files are structured:

+ -- Project1
| + -- models
| | + -- model.php
| + -- views
| | + -- view.php
| + -- controllers
| | + -- controller.php
+ -- Project2
| + -- models
| | + -- model.php
| + -- views
| | + -- view.php
| + -- controllers
| | + -- controller.php


So, while there are a lot of articles and blogs about what models, views, and controllers are, I haven't seen anything about how to set this up past what I've gathered above.

I have no idea how this becomes integrated in relation to my public folder on my server. Here is my guess, assuming that
/var/www/html/
is my public directory.

+ -- var
| + -- www
| | + -- html
| | | + -- my-application-1.php
| | | |
| | | + -- my-application-2.php
| | + -- Project1
| | | + -- models
| | | | + -- model.php
| | | + -- views
| | | | + -- view.php
| | | + -- controllers
| | | | + -- controller.php
| | + -- Project2
| | | + -- models
| | | | + -- model.php
| | | + -- views
| | | | + -- view.php
| | | + -- controllers
| | | | + -- controller.php


I am assuming based on what I've read (maybe I'm wrong) that people put their entire project "behind" their public folder. I am also assuming that you take your final application file (
my-application1.php
or
my-application-2.php
), put it in your public folder for people on the internet to use, and reference (php's
include
/
require
) your models, views and controllers from that application file. I've never referenced files behind my public file, but I'm assuming a lot here.

Can someone verify any of this for me? How is this supposed to be set up in relation to the rest of my linux server? Is there anything I am assuming that is incorrect that you'd care to correct?

I am trying to gain a good understanding of this before I pick up any frameworks (probably leaning toward Zend, Laravel, or CakePHP). I have read that frameworks help with this, but I currently don't have the resources to set up a framework and I would like to gain a full understanding of this before trying to pitch this to my team.

Answer

You're saying in your question that you don't have the resources to set up a framework but yet you have to resources to build one from scratch without even having the slightest idea what you're doing?

Believe me, you don't want to do it yourself. At the very least, you should first try some existing framework like symfony2 or zf2 or whatever you like.

When you'll become familiar with some of these and understand how they work, you will be able to create your own and give up in front of the tons of work it require.

Anyway, to answer the question, here's how zend framework 1 is structured

project
|-- application
|   |-- Bootstrap.php
|   |-- configs
|   |   `-- application.ini
|   |-- controllers
|   |   |-- ErrorController.php
|   |   `-- IndexController.php
|   |-- models
|   `-- views
|       |-- helpers
|       `-- scripts
|           |-- error
|           |   `-- error.phtml
|           `-- index
|               `-- index.phtml
|-- library
|-- public
|   |-- .htaccess
|   `-- index.php

With this framework, you would setup your web root to /path/to/project/public. As you can see, none of the application code is directly accessible from the web. All incoming requests come throught the index.php where all the magic is.

If you're interested in knowing what the magic really is, all the php web framework are open source so feel free to check it out for yourself :

ZF2 on github

Sf2 on guthub