Advertisement
MBrendecke

MVC

Apr 3rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. .htacces:
  2. Options -MultiViews
  3.  
  4. RewriteEngine on
  5. RewriteBase /public
  6.  
  7. RewriteCond %{REQUEST_FILENAME} !-d
  8. RewriteCond %{REQUEST_FILENAME} !-f
  9. RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
  10.  
  11. public/index.php:
  12. <?php
  13.  
  14. define('DS', DIRECTORY_SEPARATOR);
  15.  
  16. require_once '..' . DS . 'app' . DS . 'init.php';
  17.  
  18. app/init.php:
  19. <?php
  20.  
  21. require_once 'core/App.php';
  22.  
  23. core/App.php:
  24. <?php
  25.  
  26. class App {
  27.  
  28.     protected $controller = 'home';
  29.     protected $method = 'index';
  30.     protected $params = [];
  31.  
  32.     function __construct() {
  33.         $url = $this->parseUrl();
  34.                
  35.         if (file_exists('../app/controllers/' . "$url[0].php")) {
  36.             $this->controller = $url[0];
  37.             unset($url[0]);
  38.         }
  39.        
  40.         require_once '../app/controllers/' . "{$this->controller}.php";
  41.         $this->controller = new $this->controller;
  42.        
  43.         if(isset($url[1]) && method_exists($this->controller, $url[1])) {
  44.             $this->method = $url[1];
  45.             unset($url[1]);
  46.         }
  47.        
  48.         $this->params = $url ?? [];
  49.        
  50.         call_user_func_array([$this->controller, $this->method], $this->params);                
  51.     }
  52.  
  53.     function parseUrl() {
  54.         if (isset($_GET['url'])) {
  55.             return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
  56.         }
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement