For a PHP MVC application, what is the difference of the job of the index.php
file and front-controller? Is the front-controller in the index.php
, or is it in a separate file? How do I separate the two and let them work together? Is the front-controller supposed to be a class (or like its own entity)? (If that's the case, then index.php will instantiate the front-controller?)
I know that they have to "set up the environment," which includes defining some constants and etc, but what does what? (-- autoloader, debug stuff, etc.)
I have seen this: MVC with a front controller confusion, but that doesn't solve the problem of the difference between index.php
and the front-controller.
Actually,
index.php
should not contain any meaningful code at all, since it would be only part of your site, that is located insideDOCUMENT_ROOT
of webserver. It's content should actually look something like:It should only include a file outside
DOCUMENT_ROOT
. And that's all.This way, if something goes horribly wrong (like, php extension fails after server update) and visitors are exposed to raw php code, it will not reveal any sensitive details.
The point of Front Controller is handle all user input, turn it into a consumable form and, based on it, dispatch a command (usually in a form of method call on an object). In languages like Java, where everything must be contained in a class, a front controller would be a class. But in php you do not have this restriction.
Instead the front controller will end up being part of your bootstrap stage of the application:
Also, you should keep in mind that concept of front controller is neither made-for nor demanded-by application that attempt to implement MVC or MVC-inspired architecture.