Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. .
  2. +-- classes
  3. | +-- Database.php
  4. | +-- Route.php
  5. +-- controllers
  6. | +-- AboutUs.php
  7. | +-- Controller.php
  8. +-- views
  9. | +-- about-us.php
  10. +-- .htaccess
  11. +-- index.php
  12. +-- routes.php
  13.  
  14. <?php
  15.  
  16. class Database{
  17.  
  18. public static $host = 'localhost';
  19. public static $dbName = 'cms';
  20. public static $username = 'root';
  21. public static $password = '';
  22.  
  23. private static function connect(){
  24. $pdo = new PDO('mysql:host='.self::$host.';dbname='.self::$dbName.';charset=utf8', self::$username, self::$password);
  25. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26. return $pdo;
  27. }
  28.  
  29. public static function query($query, $params = array()){
  30. $statement = self::connect()->prepare($query);
  31. $statement->execute($params);
  32. if(explode(' ', $query)[0] == 'SELECT'){
  33. $data = $statement->fetchAll();
  34. return $data;
  35. }
  36. }
  37. }
  38.  
  39. ?>
  40.  
  41. <?php
  42.  
  43. class Route {
  44. public static function get($route, $function){
  45. //get method, don't continue if method is not the
  46. $method = $_SERVER['REQUEST_METHOD'];
  47. if($method !== 'GET'){ return; }
  48.  
  49. //check the structure of the url
  50. $struct = self::checkStructure($route, $_SERVER['REQUEST_URI']);
  51.  
  52. //if the requested url matches the one from the route
  53. //get the url params and run the callback passing the params
  54. if($struct){
  55. $params = self::getParams($route, $_SERVER['REQUEST_URI']);
  56. $function->__invoke($params);
  57.  
  58. //prevent checking all other routes
  59. die();
  60. }
  61. }
  62.  
  63. public static function urlToArray($url1, $url2){
  64. //convert route and requested url to an array
  65. //remove empty values caused by slashes
  66. //and refresh the indexes of the array
  67. $a = array_values(array_filter(explode('/', $url1), function($val){ return $val !== ''; }));
  68. $b = array_values(array_filter(explode('/', $url2), function($val){ return $val !== ''; }));
  69.  
  70. //debug mode for development
  71. if(true) array_shift($b);
  72. return array($a, $b);
  73. }
  74.  
  75. public static function checkStructure($url1, $url2){
  76. list($a, $b) = self::urlToArray($url1, $url2);
  77.  
  78. //if the sizes of the arrays don't match, their structures don't match either
  79. if(sizeof($a) !== sizeof($b)){
  80. return false;
  81. }
  82.  
  83. //for each value from the route
  84. foreach ($a as $key => $value){
  85.  
  86. //if the static values from the url don't match
  87. // or the dynamic values start with a '?' character
  88. //their structures don't match
  89. if($value[0] !== ':' && $value !== $b[$key] || $value[0] === ':' && $b[$key][0] === '?'){
  90. return false;
  91. }
  92. }
  93.  
  94. //else, their structures match
  95. return true;
  96. }
  97.  
  98. public static function getParams($url1, $url2){
  99. list($a, $b) = self::urlToArray($url1, $url2);
  100.  
  101. $params = array('params' => array(), 'query' => array());
  102.  
  103. //foreach value from the route
  104. foreach($a as $key => $value){
  105. //if it's a dynamic value
  106. if($value[0] == ':'){
  107. //get the value from the requested url and throw away the query string (if any)
  108. $param = explode('?', $b[$key])[0];
  109. $params['params'][substr($value, 1)] = $param;
  110. }
  111. }
  112.  
  113. //get the last item from the request url and parse the query string from it (if any)
  114. $queryString = explode('?', end($b))[1];
  115. parse_str($queryString, $params['query']);
  116.  
  117. return $params;
  118. }
  119. }
  120.  
  121. ?>
  122.  
  123. <?php
  124.  
  125. class AboutUs extends Controller{
  126.  
  127. }
  128.  
  129. ?>
  130.  
  131. <?php
  132.  
  133. class Controller extends Database{
  134.  
  135. public static function CreateView($viewName, $urlParams = null){
  136. require_once('./views/'.$viewName.'.php');
  137. }
  138. }
  139.  
  140. ?>
  141.  
  142. <h1>About Us</h1>
  143.  
  144. RewriteEngine On
  145.  
  146. RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
  147.  
  148. <?php
  149.  
  150. function loadClasses($class_name){
  151. $classesPath = './classes/'.$class_name.'.php';
  152. $controllersPath = './controllers/'.$class_name.'.php';
  153.  
  154. if(file_exists($classesPath)){
  155. require_once($classesPath);
  156. }else if(file_exists($controllersPath)){
  157. require_once($controllersPath);
  158. }
  159. }
  160.  
  161. spl_autoload_register(loadClasses);
  162.  
  163. require_once('routes.php');
  164. ?>
  165.  
  166. <?php
  167.  
  168. Route::get('/about-us', function(){
  169. AboutUs::CreateView('about-us');
  170. });
  171.  
  172. ?>
  173.  
  174. //check the structure of the url
  175. $struct = self::checkStructure($route, $_SERVER['REQUEST_URI']);
  176.  
  177. //if the requested url matches the one from the route
  178. //get the url params and run the callback passing the params
  179. if($struct){
  180. $params = self::getParams($route, $_SERVER['REQUEST_URI']);
  181. $function->__invoke($params);
  182.  
  183. //prevent checking all other routes
  184. die();
  185. }
  186.  
  187. /users/:username
  188. /users/:username/photos
  189. /users/:username/photos/:photoId
  190. /users/:username/photos/:photoId?showComments=false
  191.  
  192. /users/jake1990
  193. /users/jake1990/photos
  194. /users/jake1990/photos/931280134
  195. /users/jake1990/photos/931280134?showComments=false
  196.  
  197. Array -> [params => [username => jake1990], query: []];
  198. Array -> [params => [username => jake1990], query: []];
  199. Array -> [params => [username => jake1990, photoId => 931280134], query: []];
  200. Array -> [params => [username => jake1990, photoId => 931280134], query: [showComments => false]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement