Advertisement
Guest User

App

a guest
Jan 25th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. <?php defined('DIRECT') OR exit('No direct script access allowed');
  2.  
  3. class App {
  4. protected $config;
  5. protected $controller;
  6. protected $method;
  7. protected $params = [];
  8. protected $routes;
  9. protected $url;
  10.  
  11.  
  12. public function __construct()
  13. {
  14.  
  15. $this->controller = DEFAULT_CONTROLLER;
  16. $this->method = DEFAULT_METHOD;
  17.  
  18. // Getting current URL
  19. $this->url = $this->parseURL();
  20.  
  21. // Getting routes
  22. $this->routes = $this->loadFile(APP_DIR . 'config/routes');
  23.  
  24. // Getting Controller
  25. $this->set_controller($this->url);
  26.  
  27. // Getting Method
  28. $this->set_action($this->url);
  29.  
  30. // Getting parameters
  31. if(!$this->params)
  32. $this->set_params($this->url);
  33.  
  34. // Composer Autoload
  35. if(@$this->config['composer'] == true) {
  36. if(file_exists('vendor/autoload.php'))
  37. require_once('vendor/autoload.php');
  38. else
  39. echo '<span style="color:#bc5858;"><strong>UYARI:</strong> Composer yükleyicisi bulunamadı.</span>';
  40. }
  41.  
  42. call_user_func_array([$this->controller, $this->method], $this->params);
  43. }
  44.  
  45. private function set_controller($url)
  46. {
  47. if(isset($url[0])) {
  48. if (file_exists(APP_DIR . 'controllers/' . $this->makeURL($url[0]) . '.php')) {
  49. $this->controller = $this->makeURL($url[0]);
  50. $this->loadFile(APP_DIR . 'controllers/' . $this->controller);
  51. $this->controller = new $this->controller;
  52. } else {
  53. if (array_key_exists($this->makeURL($url[0]), $this->routes)) {
  54. $route = explode('/', $this->routes[$this->makeURL($url[0])]);
  55. $this->controller = $route[0];
  56. $this->loadFile(APP_DIR . 'controllers/' . $this->controller);
  57. $this->controller = new $this->controller;
  58. } else {
  59. $this->loadFile(APP_DIR . 'views/errors/error_404');
  60. die();
  61. }
  62. }
  63. } else {
  64. $this->loadFile(APP_DIR . 'controllers/' . $this->controller);
  65. $this->controller = new $this->controller;
  66. }
  67. }
  68.  
  69. private function set_action($url)
  70. {
  71. if (isset($url[1])) {
  72. if ($url[1] != 'index') {
  73. if (method_exists($this->controller, $url[1])) {
  74. $this->method = $url[1];
  75. } else {
  76. unset($url[0]);
  77. if(method_exists($this->controller, $this->method)) {
  78. $this->params = array_values($url);
  79. } else {
  80. $this->loadFile(APP_DIR . 'views/errors/error_404');
  81. die();
  82. }
  83. }
  84. }
  85. unset($this->url[1]);
  86. } else {
  87. if (array_key_exists($this->makeURL($url[0]), $this->routes)) {
  88. $route = explode('/', $this->routes[$this->makeURL($url[0])]);
  89. if(isset($route[1])) {
  90. if (method_exists($this->controller, $route[1])) {
  91. $this->method = $route[1];
  92. } else {
  93. $this->loadFile(APP_DIR . 'views/errors/error_404');
  94. die();
  95. }
  96. }
  97. }
  98. }
  99. unset($this->url[0]);
  100. }
  101.  
  102. private function set_params($url)
  103. {
  104. $this->params = $url ? array_values($url) : [];
  105. }
  106.  
  107. public function parseURL()
  108. {
  109. if (isset($_GET['url'])) {
  110. return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
  111. }
  112. }
  113.  
  114. public function makeURL($queryString)
  115. {
  116. $queryString = ucwords(strtolower(str_replace(['-','_','%20'], [' ',' ',' '], $queryString)));
  117. $queryString = str_replace(' ', '_', $queryString);
  118. return $queryString;
  119. }
  120.  
  121. public function loadFile($fileName) {
  122. $fileName = $fileName . '.php';
  123. if (file_exists($fileName)) {
  124. return require_once $fileName;
  125. } else {
  126. $code = 1005;
  127. $text = 'Böyle bir dosya bulunmamaktadır. {' . $fileName . '}';
  128. require_once APP_DIR . 'views/errors/error_system.php';
  129. die();
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement