MBrendecke icon

MVC

MBrendecke | PRO | 04/03/18 05:06:38 PM UTC | 0 ⭐ | 375 👁️ | Never ⏰ | []
PHP |

1.36 KB

|

None

|

0 👍

/

0 👎

.htacces:
Options -MultiViews
 
RewriteEngine on
RewriteBase /public
 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
 
public/index.php:
<?php
 
define('DS', DIRECTORY_SEPARATOR);
 
require_once '..' . DS . 'app' . DS . 'init.php';
 
app/init.php:
<?php
 
require_once 'core/App.php';
 
core/App.php:
<?php
 
class App {
 
    protected $controller = 'home';
    protected $method = 'index';
    protected $params = [];
 
    function __construct() {
        $url = $this->parseUrl();
                
        if (file_exists('../app/controllers/' . "$url[0].php")) {
            $this->controller = $url[0];
            unset($url[0]);
        }
        
        require_once '../app/controllers/' . "{$this->controller}.php";
        $this->controller = new $this->controller;
        
        if(isset($url[1]) && method_exists($this->controller, $url[1])) {
            $this->method = $url[1];
            unset($url[1]);
        }
        
        $this->params = $url ?? [];
        
        call_user_func_array([$this->controller, $this->method], $this->params);                
    }
 
    function parseUrl() {
        if (isset($_GET['url'])) {
            return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
        }
    }
 
}

Comments