Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've made an "simple" / "small" MVC-like PHP system. I've used some code of an project I used before and ask questions about it back then to.

The thing is, it works (the admin part to begin with), but I'm not sure about the quality of the code. I know that there are mainly no comments, but they will be added when I'm sure that I stick with the current version.

Here's an link to an Github page containing all the source code

This is /admin/index.php

<?php

session_start();

require_once 'autoload.php';

// Initialize the log class which sets the error logger
$log = new Log();

$router = new Router();
$app = $router->startRouting();

echo $app;

/admin/autoload.php

<?php

require_once 'config.php';
require_once 'filters.php';

/*****************
 *               *
 *   Libraries   *
 *               *
 *****************/
require_once DIR_LIB . 'BaseController.php';
require_once DIR_LIB . 'BaseModel.php';
require_once DIR_LIB . 'db.php';
require_once DIR_LIB . 'language.php';
require_once DIR_LIB . 'log.php';
require_once DIR_LIB . 'minify.php';
require_once DIR_LIB . 'router.php';
require_once DIR_LIB . 'view.php';

/***************
 *             *
 *   Helpers   *
 *             *
 ***************/
require_once DIR_HELPER . 'func.php';
require_once DIR_HELPER . 'url.php';

/**************
 *            *
 *   Vendor   *
 *            *
 **************/
require_once '../vendor/autoload.php';

/DIR_LIB/router.php

<?php

class Router
{
    private $get, $post, $request, $cookie, $server, $routeExp;

    public function startRouting()
    {
        $this->routeExp = explode("/", rtrim((isset($this->get['_route_']) ? $this->get['_route_'] : "home/index"), "/"));

        if (!isset($this->routeExp[1])) {
            $this->routeExp[1] = "index";
        }

        // Let's start the routing filter
        Filter::route($this->routeExp);

        if (!file_exists(DIR_CTRL . $this->routeExp[0] . ".php")) {
            $view = new View();
            echo $view->generate404();
            die();
        }

        $app = $this->loadController($this->routeExp[0]);

        if (!method_exists($app, $this->routeExp[1])) {
            trigger_error("Error: Could not load method " . $this->routeExp[1] . " inside class " . ucfirst($this->routeExp[0] . 'Controller'));
        }
        $method = $this->routeExp[1];

        return $app->$method($this->getParams());
    }

    private function loadController($name)
    {
        require_once DIR_CTRL . $name . '.php';
        $controllerName = ucfirst($name) . "Controller";
        return new $controllerName();
    }

    private function getParams()
    {
        $params = $this->routeExp;
        unset($params[0], $params[1]);
        return array_values($params);
    }

    /**
     * This construct will make all incoming data "clean"
     */
    public function __construct()
    {
        $_GET = $this->clean($_GET);
        $_POST = $this->clean($_POST);
        $_REQUEST = $this->clean($_REQUEST);
        $_COOKIE = $this->clean($_COOKIE);
        $_SERVER = $this->clean($_SERVER);

        $this->get = $_GET;
        $this->post = $_POST;
        $this->request = $_REQUEST;
        $this->cookie = $_COOKIE;
        $this->server = $_SERVER;
    }

    /**
     * This recursive function will make the input clean through htmlspecialchars()
     *
     * @param $data array|string
     * @return array|string
     */
    private function clean($data)
    {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                unset($data[$key]);
                $data[$this->clean($key)] = $this->clean($value);
            }
        } else {
            $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
        }

        return $data;
    }
}

/filters.php

<?php

class Filter{

    private static $route;

    // Filter at route level
    public static function route($routeExp){
        self::$route = $routeExp;

        // Make sure that the admin is logged
        if(!isset($_SESSION['isAdmin']) || $_SESSION['isAdmin'] != true){
            if(self::$route[0] != 'login'){
                Url::navigate('login');
            }
        }
    }

    public static function beforeViewing(){

    }
}

These are some important files, but if you want me to show some more files, let me know.

Anything that could help / guide me to improve the code would help me a lot. I'm still rather new to this whole code separation trough OOP / MVC-like thing and want to learn it in the best way possible.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.