Recently i've been learning how to implement an MVC structure in my applications. I'm planning to use a PHP framework in the near future, but for now it's just pure PHP. I've learned that controllers should be 'skinny' and models should be 'fat', but can't seem to understand how this can go hand in hand with clean URLs.
At the company where I'm doing my internship I'm developing a web-based application to perform CRUD actions on 3 different tables in a database. Each of the tables has its own specific fields, which means that for each of those tables there are 4 views needed (1 for each CRUD action).
I want to make use of clean URL's, so right now i have 1 (only one) controller (index.php) which splits up the URL in segments and analyzes them, calling the specific methods and views for each segment, or (if needed) redirecting the user to a correct URL.
Right now the controller contains close to 1000 lines of code. I always read that controllers should be 'skinny', and see people talking about multiple controllers in one application. I can't seem to understand how to implement this in combination with clean URLs...
Any help would be much appreciated. At the company I'm doing my internship at there's just a web designer, so I can't ask him any programming-related questions...
Below is a basic representation of my controller (index.php)
// Check if user is logged in
if (isset($_SESSION['username']) AND isset($_SESSION['loggedIn'])) {
// Get URL from $_SERVER
$url = $_SERVER['REQUEST_URI'];
// Split URL and assign values to $url
$url = trim($url, "/");
$url = explode('/', $url);
// Remove first url segment (index.php) for convenience
array_shift($url);
// Check if 1st URL segment (category) is set
if (isset($url[1])) {
$category = $url[1];
// Check if category is 'apples'
if ($category == 'apples') {
// Check if 2nd URL segment (action) is set
if (isset($url[2])) {
$action = $url[2];
// Check if action is 'add'
if ($action == 'add') {
// Calls to Model
// Include Presentation (form to add new record to 'apples' category)
}
// Check if action is 'view'
elseif ($action == 'view') {
// Calls to Model
// Include Presentation (list of all records in the 'apples' table)
}
// Check if action is 'edit'
elseif ($action == 'edit') {
// Check if 3d URL segment (id) is set
if (isset($url[3])) {
$id = $url[3];
// Calls to Model
// Include Presentation (form to edit record of the 'apples' category);
}
// If 3d URL segment (id) is not set then redirect
else {
header('Location: index.php/$category/view');
}
}
// Check if action is 'delete'
elseif ($action == 'delete') {
// Check if 3d URL segment (id) is set
if (isset($url[3])) {
$id = $url[3];
// Calls to Model
// Include Presentation (form to edit record of the 'apples' category);
}
// If 3d URL segment (id) is not set then redirect
else {
header('Location: index.php/$category/view');
}
}
// If 2nd URL segment (action) is invalid then assume user wants to view and redirect
else {
header("Location: index.php/$category/view");
}
}
// If 2nd URL segment (category) is not set then assume user wants to view and redirect
else {
header("Location: index.php/$category/view");
}
}
// Check if category is 'pineapples'
elseif ($category == 'pineapples') {
// same here as in 'apples' code block
}
// Check if category is 'pears'
elseif ($category == 'pears') {
// same here as in 'apples' code block
}
// If 1st URL segment (category) is invalid then redirect to index.php
else {
header('Location: index.php')
}
}
// If 1st URL segment (category) is not set then show category overview
else {
include 'presentation/category_overview.php';
}
}
// If user is not logged in then check if login form got submitted
elseif($_POST['formSubmit'] == 'submit') {
// Calls to Model (form and user credentials validation)
// Include Presentation (category overview)
}
// If user is not logged in and did not submit login form then include view (login form)
else {
include 'presentation/login.php';
}