Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Load;
- class Loader {
- private static
- $_controller = null,
- $_method = null,
- $_params = [];
- private function __construct() {
- }
- public static function start() {
- self::autoload();
- self::defaultRouter();
- }
- private static function autoload() {
- spl_autoload_register(function($class) {
- $explodedPath = explode("\\", trim($class, '\\'));
- $count = count($explodedPath);
- $filename = DIRECTORY_SEPARATOR . ucfirst(strtolower($explodedPath[$count - 1])) . '.php';
- unset($explodedPath[$count - 1]);
- $path = implode(DIRECTORY_SEPARATOR, $explodedPath);
- if (file_exists($path . $filename)) {
- require_once $path . $filename;
- } else {
- throw new \Exception("Page not found", "500");
- }
- });
- }
- private static function defaultRouter() {
- self::$_controller = \Models\Config::default_controller;
- self::$_method = \Models\Config::default_method;
- $url = explode("/", \Models\Sanitaze::clear(rtrim($_GET['url'], '/')));
- if (isset($url[0]) && $url[0] != '') {
- self::$_controller = "\\Controllers\\" . $url[0];
- unset($url[0]);
- }
- self::$_controller = new self::$_controller;
- if (isset($url[1]) && $url[1] != '') {
- if (method_exists(self::$_controller, $url[1])) {
- self::$_method = $url[1];
- unset($url[1]);
- }
- }
- self::$_params = $url ? array_values($url) : [];
- call_user_func_array([self::$_controller, self::$_method], self::$_params);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement