Commit b8edf1f7 authored by thibaut-felten's avatar thibaut-felten

Request maj

parent 92205143
<?php <?php
// define __ROOT_DIR constant which contains the absolute path on disk // define __ROOT_DIR constant which contains the absolute path on disk
// of the directory that contains this file (index.php) // of the directory that contains this file (index.php)
// e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/index.php => __ROOT_DIR = /home/luc.fabresse/public_html // e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/index.php => __ROOT_DIR = /home/luc.fabresse/public_html
$rootDirectoryPath = realpath(dirname(__FILE__)); $rootDirectoryPath = realpath(dirname(__FILE__));
define ('__ROOT_DIR', $rootDirectoryPath ); define ('__ROOT_DIR', $rootDirectoryPath );
// Load all application config // Load all application config
require_once(__ROOT_DIR . "/config/config.php"); require_once(__ROOT_DIR . "/config/config.php");
...@@ -15,7 +13,6 @@ ...@@ -15,7 +13,6 @@
// Reify the current request // Reify the current request
$request = Request::getCurrentRequest(); $request = Request::getCurrentRequest();
Response::interceptEchos(); Response::interceptEchos();
try { try {
$controller = Dispatcher::dispatch($request); $controller = Dispatcher::dispatch($request);
$response = $controller->execute(); $response = $controller->execute();
......
...@@ -14,31 +14,31 @@ private function load($className) { ...@@ -14,31 +14,31 @@ private function load($className) {
// it is in one of these subdirectory '/classes/', '/model/', '/controller/' // it is in one of these subdirectory '/classes/', '/model/', '/controller/'
// if it is a model, load its sql queries file too in sql/ directory // if it is a model, load its sql queries file too in sql/ directory
if(strpos($className, "Controller") !==FALSE){ if(strpos($className, "Controller") !==FALSE){
$classFile .= "../controller/"; $classFile .= __ROOT_DIR . "/controller/";
}else if(strpos($className, "Model") !==FALSE){ }else if(strpos($className, "Model") !==FALSE){
$classFile .= "../model/"; $classFile .= __ROOT_DIR . "/model/";
}else if(strpos($className, "Role") !==FALSE){ }else if(strpos($className, "Role") !==FALSE){
$classFile .= "../model/"; $classFile .= __ROOT_DIR . "../model/";
}else if(strpos($className, "User") !==FALSE){ }else if(strpos($className, "User") !==FALSE){
$classFile .= "../model/"; $classFile .= __ROOT_DIR . "/model/";
}else if(strpos($className, "Response") !==FALSE){ }else if(strpos($className, "Response") !==FALSE){
$classFile .= "../classes/"; $classFile .= __ROOT_DIR . "/classes/";
}else if(strpos($className, "Request") !==FALSE){ }else if(strpos($className, "Request") !==FALSE){
$classFile .= "../classes/"; $classFile .= __ROOT_DIR . "/classes/";
}else if(strpos($className, "Dispatcher") !==FALSE){ }else if(strpos($className, "Dispatcher") !==FALSE){
$classFile .= "../classes/"; $classFile .= __ROOT_DIR . "/classes/";
}else if(strpos($className, "DatabasePDO") !==FALSE){ }else if(strpos($className, "DatabasePDO") !==FALSE){
$classFile .= "../classes/"; $classFile .= __ROOT_DIR . "/classes/";
} }
$classFile.=.$className.".class.php"; $classFile.=$className.".class.php";
include $classFile; include $classFile;
} }
} }
......
...@@ -3,9 +3,13 @@ class Request { ...@@ -3,9 +3,13 @@ class Request {
protected $controllerName; protected $controllerName;
protected $uriParameters; protected $uriParameters;
protected static $request = NULL;
public static function getCurrentRequest(){ public static function getCurrentRequest(){
// T if(is_null(static::$request))
static::$request = new static();
return static::$request;
} }
public function __construct() { public function __construct() {
...@@ -17,7 +21,8 @@ class Request { ...@@ -17,7 +21,8 @@ class Request {
// e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php => __BASE_URI = /~luc.fabresse // e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php => __BASE_URI = /~luc.fabresse
// e.g. http://localhost/CDAW/api.php => __BASE_URI = /CDAW // e.g. http://localhost/CDAW/api.php => __BASE_URI = /CDAW
protected function initBaseURI() { protected function initBaseURI() {
$this->baseURI = // TODO $url = dirname($_SERVER['SCRIPT_NAME']);
$this->baseURI = $url;
} }
// intialise controllerName et uriParameters // intialise controllerName et uriParameters
...@@ -30,9 +35,20 @@ class Request { ...@@ -30,9 +35,20 @@ class Request {
// => controllerName == 'user' // => controllerName == 'user'
// uriParameters == [ 1 ] // uriParameters == [ 1 ]
protected function initControllerAndParametersFromURI(){ protected function initControllerAndParametersFromURI(){
$prefix = $_SERVER['SCRIPT_NAME'];
$uriParameters = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$i=0;
while($i<strlen($prefix) && $i<strlen($uriParameters))
if($prefix[$i]===$uriParameters[$i]){
$i++;
}
$uriParameters = substr($uriParameters, $i);
$this->controllerName = // TODO $uriParameters = trim($uriParameters, '/');
$this->uriParameters = // TODO $uriSegments = explode('/', $uriParameters);
$this->controllerName = array_shift($uriSegments) ?: "default";
$this->uriParameters = $uriSegments;
} }
// ============== // ==============
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment