For many days, I've been re-designing my application, applying some custom pattern and making our code looks like a framework. I have something like this for separate the business logic, from views:
if(file_exists($route . $urlpara[0] . ".template.php")){
require_once($route . $urlpara[0] . ".functions.php");
require_once($route . $urlpara[0] . ".template.php");
}else{
// 404 error
}
$route
means the route.
$urlpara[0]
means a parsed URL, where $urlpara[0]
should be in this case the URL. In this case, we could say $urlpara[0] = index
. So this would like like this without variables:
if(file_exists("app/index.template.php")){
require_once("app/index.functions.php");
require_once("app/index.template.php");
}else{
// 404 error
}
Inside index.template goes the view, and inside index.functions goes the model.
Here's the .htaccess
RewriteEngine On
Options -MultiViews
Options +FollowSymlinks
RewriteBase /script/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Basic params MVC
RewriteRule ^(.*)$ index.php?page=$1 [L]
Here the index.php (the part where it reads and splits up the model/view):
$exploded = explode('/', $_GET['page']); // pagename/param1/param2/etc..
$route = 'script/path/';
$first_param = $exploded[0];
$second_param = $exploded[1];
// Controller
if(file_exists($route . $first_param . ".template.php")){
require_once($route . $first_param . ".functions.php");
require_once($route . $urlpara[0] . ".template.php");
}else{
// 404 error
}
// now it checks if param1 = pagename exists. For example dashboard.template.php. Same with dashboard.functions.php
function generateTitle(){
echo 'this title generated dynamically';
}
Within dashboard.template.php we find this:
<!-- View -->
<html>
<head>
<title><?php echo generateTitle(); ?></title>
</head>
<?php
foreach($whatever as $some){
echo $some;
}
?>
</html>
And within the dashboard.functions.php we find this:
<?php
// Model
$whatever = array('test', 'test2', 'test3');
?>
I don't really know if I'm doing it well. Could be this represented as a MVC pattern?