I have setup two different PHP systems this summer. Each uses two different methods:
Method #1: One PHP File Per Task
This method requires that a PHP
file be created for each major task. For example, my upload script would be access via http://www.domain.com/upload.php
. On upload.php
, a "controller" and a "view" class are instantiated and used. For example, upload.php
might look something like this:
<?php
require_once PATH_LIBRARY . 'control/Uploader.class.php';
require_once PATH_LIBRARY . 'view/UploaderPage.class.php';
$uploader = new Uploader();
$uploader->setView(new UploaderPage());
$uploader->init();
?>
In the above script, if I wanted to call on another script, I would simply redirect and append the neccessary URL encoded variables (header('Location: edit_details.php?image_id=456');
).
Method #2: A main index.php
file that handles all requests
This method does not require that a PHP
file be created for each major task. Instead, each "controller" class that will be used in the system is registered on index.php
with a unique class that I call Router
. The router decides if the specified Controller is legal or illegal and acts accordingly (routes the browser to the correct controller). For example, a simplified version of the index.php
script:
<?php
require_once 'bootstrap.inc';
require_once PATH_LIBRARY . 'router/Router.class.php';
$router = new Router();
$router->register('Uploader', PATH_LIBRARY . 'control/Uploader.class.php');
$router->register('DetailsEditor', PATH_LIBRARY . 'control/DetailsEditor.class.php');
$router->route();
?>
Thus, every action takes place at index.php
. There is no need for a lot of files which don't do much else that instantiate a specific view and controller class. BUT, if you want to call script/class A from script/class B, then you need to pass the controller class's name along on the URL: header('Location: index.php?controller=DetailsEditor&image_id=456')
.
So far, I don't really like the fact that I need to include the Controller name in the URL. I feel that it exposes too much of my underlying system to the end-user. But, I do like the fact that I can register all controller classes on one page. Mostly, I don't know if exposing the controller name is safe or not. One other annoyance is that, if I want to call on a script via POST requests, I have to include a hidden input that specifies the required controller class (e.g. <input type="hidden" name="controller" value="DetailsEditor" />
).
I hope that is enough to go on. I am just nervous that the second method is not really going to serve me well in the months ahead. I have a small window of time soon to choose one or the other.
Thank-you.