index.php - lddefensor/SimpleAI GitHub Wiki

This is the landing action of the framework. Using Apache's mod_rewrite, all requests to the application is handled by index.php.

Application's .htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    webroot/    [L]
        RewriteRule    (.*) webroot/$1    [L]
    </IfModule>
  • all urls are routed to webroot

webroot's .htaccess

   <IfModule mod_rewrite.c> 
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|ico|js|html)$ [NC]
      RewriteRule ^(.*)$ index.php?/ [QSA,L] 
   </IfModule>
  • all files not defined in jpg|css|js|gif|png|ico|js|html are processed by index.php

Displaying Errors during Development

error_reporting(E_ALL); // TODO Change to 0 on production MODE
ini_set("display_errors", "on"); // TODO Change to off on production mode
  • the first two lines of index.php allows displaying of errors. This is important during development phase. Developers should simply update the first two lines to disable displaying of errors.

headers

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');  
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");  
  • the next lines allow HTTP CORS. for applications that uses the framework as restful API's this is important. Otherwise, developers can comment these lines.

Initial Constants

if (!defined('DS'))	
	define('DS', DIRECTORY_SEPARATOR);

$FILE = __FILE__;

if (!defined('ROOT')) 
	define('ROOT',  dirname($FILE, 2)); 

if(!defined('APP_ROOT'))
	define('APP_ROOT', ROOT. DS. 'Ulap');

if(!defined('LIB'))
	define('LIB', APP_ROOT. DS. 'Lib'); 
 
 
//URL is usually the container of the ROOT
$URL = str_replace(dirname($FILE, 3) . DS, '', ROOT);
if(!defined('URL'))
	define('URL',  '/'.$URL);
  • as much as possible, do not edit the following lines as these constants are used by the application everytime to include another file

Required files

require( ROOT . DS . 'Config' . DS . 'defaults.php');
require( LIB . DS . 'Router.php');

Defauts.php

  • the file allows user to define custom constants

Router.php

  • the Class that parses the url into routes. Consumable by Controller Classes

Query String

$queryString = urldecode(str_replace(URL.'/' , '', $_SERVER["REQUEST_URI"]));  

$MyRouter = new Ulap\Router($queryString); 
  • the string that we are interested in is the string following your application's url.
  • this is the argument needed by the Router Class

Exception Handler

$MyRouter->ExceptionHandler = new ApiErrorHandler();
  • the router class accepts a property ExceptionHandler. This is a class that handles exception thrown by Router Class. You can leave this undefined as the application's default exception handler is already defined.

Initializing route

$MyRouter->route();   
  • this method initiates the route routine.
⚠️ **GitHub.com Fallback** ⚠️