Hallo,

Vorige week ben ik weer begonnen met het leren maken van een website d.m.v. MVC en daar is de volgende bootstrap uitgekomen:


<?php
// Class Bootstrap
class Bootstrap
{
    // Initialize variables
    private $_controller;

    // Constructor
    public function __construct()
    {
        // Explode url
        $_url = explode('/', $_GET['url']);

        // Check if url[0] is empty
        if (empty($_url[0]))
        {
            // Require controller
            require PATH_ROOT . '/controllers/index.php';

            // Create controller
            $this->_controller = new IndexController();
            $this->_controller->index();
        }
        else
        {
            // Initialize variables
            $_file = PATH_ROOT . '/controllers/' . $_url[0] . '.php';
            $_class = $_url[0] . 'Controller';
            $_arguments = array_slice($_url, 2);

            // Check if file exists
            if (file_exists($_file))
            {
                // Require controller
                require $_file;

                // Create controller
                $this->_controller = new $_class;
                $this->_controller->loadModel($_url[0]);

                // Check if method exists
                if (method_exists($this->_controller, $_url[1]))
                {
                    // Execute method
                    $this->_controller->{$_url[1]}($_arguments);
                }
                else
                {
                    // Execute method
                    $this->_controller->index($_arguments);
                }
            }
            else
            {
                // Require controller
                require PATH_ROOT . '/controllers/error.php';

                // Create controller
                $this->_controller = new ErrorController();
                $this->_controller->index($_arguments);
            }
        }
    }
}
?>


Het werkt zoals ik wil, maar ik heb geen idee of ik dit op een juiste manier heb gedaan, hebben jullie tips wat ik er beter aan kan?

Alvast bedankt!
Geef $_GET['url'] als parameter aan de constructor mee. Zo kun je dit de classe zonder aanpassingen naar een ander project kopiëren en tijdens het aanroepen $_SERVER['PATH_INFO'] gebruiken. Als je dan in de toekomst Bootstrap aanpast hoef je dat niet ook aan te passen wanneer je de Bootstrap uit het andere project vervangt.

Je hebt wel een method_exists() check, maar geen class_exists() na het requireën.
Oké, dan kom ik uit op het volgende:


<?php
// Class Bootstrap
class Bootstrap
{
    // Initialize variables
    private $_controller;
    private $_url;

    // Constructor
    public function __construct($_url)
    {
        // Explode url
        $this->_url = explode('/', $_url);

        // Check if url[0] is empty
        if (empty($this->_url[0]))
        {
            // Require controller
            require PATH_ROOT . '/controllers/index.php';

            // Create controller
            $this->_controller = new IndexController();
            $this->_controller->index();
        }
        else
        {
            // Initialize variables
            $_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
            $_class = $this->_url[0] . 'Controller';
            $_arguments = array_slice($this->_url, 2);

            // Check if file exists
            if (file_exists($_file))
            {
                // Require controller
                require $_file;

                // Check if class exists
                if (class_exists($_class))
                {
                    // Create controller
                    $this->_controller = new $_class;
                    $this->_controller->loadModel($this->_url[0]);

                    // Check if method exists
                    if (method_exists($this->_controller, $this->_url[1]))
                    {
                        // Execute method
                        $this->_controller->{$this->_url[1]}($_arguments);
                    }
                    else
                    {
                        // Execute method
                        $this->_controller->index($_arguments);
                    }
                }
                else
                {
                    // Require controller
                    require PATH_ROOT . '/controllers/error.php';

                    // Create controller
                    $this->_controller = new ErrorController();
                    $this->_controller->index($_arguments);
                }
            }
            else
            {
                // Require controller
                require PATH_ROOT . '/controllers/error.php';

                // Create controller
                $this->_controller = new ErrorController();
                $this->_controller->index($_arguments);
            }
        }
    }
}
?>


Klopt dit? En kan ik dit ook nog onderverdelen in meerdere (private) methods? Ik gebruik bijvoorbeeld 2x dit stukje:


<?php
                // Require controller
                require PATH_ROOT . '/controllers/error.php';

                // Create controller
                $this->_controller = new ErrorController();
                $this->_controller->index($_arguments);
?>


Zou ik hier bijvoorbeeld 'private function _error()' van kunnen maken?
Dat zou kunnen, wat ook kan is aan het einde een losstaande if ( ! $this->_controller) { ... } te zetten.
Uiteindelijk kom ik dan uit op dit:


<?php
// Class Bootstrap
class Bootstrap
{
    // Initialize variables
    private $_controller;
    private $_url;

    // Constructor
    public function __construct($_url)
    {
        // Explode url
        $this->_url = explode('/', $_url);

        // Check if url[0] is empty
        if (empty($this->_url[0]))
        {
            // Require controller
            require PATH_ROOT . '/controllers/index.php';

            // Create controller
            $this->_controller = new IndexController();
            $this->_controller->index();
        }
        else
        {
            // Initialize variables
            $_file = PATH_ROOT . '/controllers/' . $this->_url[0] . '.php';
            $_class = $this->_url[0] . 'Controller';
            $_arguments = array_slice($this->_url, 2);

            // Check if file exists
            if (file_exists($_file))
            {
                // Require controller
                require $_file;

                // Check if class exists
                if (class_exists($_class))
                {
                    // Create controller
                    $this->_controller = new $_class;
                    $this->_controller->loadModel($this->_url[0]);

                    // Check if method exists
                    if (method_exists($this->_controller, $this->_url[1]))
                    {
                        // Execute method
                        $this->_controller->{$this->_url[1]}($_arguments);
                    }
                    else
                    {
                        // Execute method
                        $this->_controller->index($_arguments);
                    }
                }
            }
        }

        // If no controller is set
        if (!($this->_controller))
        {
            // Require controller
            require PATH_ROOT . '/controllers/error.php';

            // Create controller
            $this->_controller = new ErrorController();
            $this->_controller->index($_arguments);
        }
    }
}
?>

Reageren