/index.php
/.htaccess
/config.ini.php
/404.php
/view
/view/test1.php
/view/test2.php
/model
/model/model.class.php
/controller
/controller/controller.class.php
/log
/log/logger.class.php
/log/log.log
/cache
/cache/cache.class.php
/cache/template.cache.class.php
/cache/template
/cache/template/test1.php.tmp
/cache/template/test2.php.tmp
The view, model, controller, log and cache directories contains the following framework modules respectively:
view directory contains our view level files. i.e. files containing our HTML, js, css code.
model directory contains the model class responsible for interacting with database and other storages
controller directory contains our controller class. Each incoming request is first received by the controller class constructor, which thereafter controls the flow of request in the framework
log directory contains our logger class. This class is auto loaded for every request providing a basic logger::log($log_message) logger method throughout the framework. This class logs all data in a file called log.log.
cache directory contains our cache class. For this blog tutorial, we will only write the template caching engine class. In production systems, we might have individual classes for other types of cache systems e.g. memcached (Read Memcached and “N” things you can do with it – Part 1 to know more about memcached and MySQL Query Cache, WP-Cache, APC, Memcache – What to choose for a complete comparison lists of various other caching techniques.
Lets see in details, what all file each and every directory contain contains.
Root directory files
We have 4 files in our root directory, namely .htaccess, index.php, config.ini.php and 404.php in order of relevance. Lets look through the content of these files:
.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
1st two lines essentially means that Switch on the apache rewrite module and set RewriteBase as / i.e. the root directory
Last 3 lines mean that, if incoming request is for a file or directory which physically exists under the root directory serve them otherwise route all other requests to index.php in the root directory
Hence now for an incoming request like http://localhost/test1.php, apache will route the request to index.php in the root directory because there is no test1.php under the root directory. Cool, lets see what index.php has to offer.
index.php
<?php
// include configuration file
include_once("config.ini.php");
// include controller files
include_once("controller/controller.class.php");
?>
index.php doesn’t do much except for including our core configuration file and controller class file. Controller class constructor is initiated as soon as the class file is included.
config.ini.php is our core configuration file. It provides the framework with an array called $config containing various information like: mysql database credentials, requested url details and various other global parameters. Lets see what all parameter does it provide us with.
config.ini.php
<?php
$config = array(
'host'=>array(
'name' => "http://".$_SERVER['HTTP_HOST'].'/',
'uri' => $_SERVER['REQUEST_URI'],
'url' => parse_url($_SERVER['REQUEST_URI']), // note it contains the parsed url contents
),
'mysql'=>array(
'host' => 'localhost',
'name'=> 'testdb',
'user' => 'root',
'pass' => 'password',
),
'cache'=>array(
'template' => 'On', // template caching switched on by default
'memcached' => 'Off', // switch off memcached caching
),
);
?>
$config[‘host’] array saves various parameter about the host itself, e.g. hostname, hosturi (the requested uri, hosturl (it contains the parse_url(hosturi)).
$config[‘mysql’] array contains mysql database parameters. However in this blog post we will not interact with databases.
$config[‘cache’] tells the framework what all caching modules are switched on.
404.php
<html>
<head>
</head>
<body>
<h1>404 Page</h1>
</body>
</html>
Controller directory files
For this blog post, controller directory consists of a single class file. i.e. controller.class.php. We saw this being included by index.php in the root folder above. As soon as controller class is included, it’s constructor is invoked. Before we dig in more, lets see the controller class file:
controller.class.php
<?php
// include logger class
include_once("log/logger.class.php");
// include cache class (contains template caching)
include_once("cache/cache.class.php");
// include model class
include_once("model/model.class.php");
class controller {
function __construct($config) {
global $config;
// generate requested template name and path
$config['template']['name'] = $config['host']['uri'] == '/' ? 'index.php' : substr($config['host']['uri'], 1, strlen($config['host']['uri']));
$config['template']['path'] = "view/".$config['template']['name'];
// check 404
if(!file_exists($config['template']['path'])) {
$config['template']['name'] = "404.php";
$config['template']['path'] = "404.php";
}
logger::log("Requested template name ".$config['template']['name'].", path ".$config['template']['path']);
// invoke template caching engine
$template_cache = new template_cache();
// include the template
include_once($config['template']['path']);
// cache template
$template_cache->setTemplate();
}
}
$controller = new controller($config);
?>
At the top, controller class includes the logger.class.php, cache.class.php and model.class.php files. At the bottom, the controller object is instantiated.
The constructor performs the following 5 tasks:
At first it generates a template name and a template path for the incoming request i.e. for http://localhost/, $config['template']['name']='index.php' and for http://localhost/test1.php, $config['template']['name']='test1.php'.
Second it checks for 404. For the above generated template path, e.g. $config['template']['path']='view/test1.php', it checks whether this file exists inside root directory. If it doesn’t template path and names are set to 404.php
Thirdly, It invokes the template caching engine. i.e. $template_cache = new template_cache();
Forth, it includes the generated template path above i.e. include_once($config['template']['path']);
Fifth and finally, it caches the generated HTML, js, css code by the template file includes above. This is achieved by the following code, $template_cache->setTemplate();
Before we move our attention to, lets see in short the content of log and view directories.
Log directory files
Log directory contains our logger class. This class is auto loaded for each incoming request that is being routed to index.php in the root directory (as we saw above). The logger.class.php provides a static logger::log($log_message) method, which can be used throughout the framework for logging messages. We will be using it everywhere.
logger.class.php
<?php
class logger {
static $log_file = "log/log.log";
static function log($log) {
if($log != '') {
$fh = fopen(self::$log_file, "a");
fwrite($fh,date('Y-m-d H:i:s')."\n".$log."\n\n");
fclose($fh);
}
}
}
?>
The logger class by default logs all data to a file called log.log.