2

I'm trying to build a CMS using the MVC pattern. The structure of my CMS is as follows:

->index.php: entry point. Contains the appropriate includes and creates the frontController which takes a router as it's parameter:

$frontController = new FrontController(new Router);
echo $frontController->output();

->router.php: analyses the URL and defines what the names of each Model,View and Controller will be. By default they are preceded by home, but if the url is of the form http://localhost/index.php?route=Register the MVC classes will be named RegisterModel, RegisterView and Register Controller.

if(isset ( $_GET ['route'] ))
{
    $this->URL = explode ( "/", $_GET ['route'] );

    $this->route = $_GET ['route'];
    $this->model = ucfirst($this->URL [0] . "Model");
    $this->view = ucfirst($this->URL [0] . "View");
    $this->controller = ucfirst($this->URL [0] . "Controller");
}
else
{
    $this->model = "HomeModel";
    $this->view = "HomeView";
    $this->controller = "HomeController";
    $this->route = "Home";
}

->frontController.php: This is where I am stuck. When I go to the homepage, it can be visualised correctly because I already have the default HomeModel,HomeView and HomeController classes created. But I created a link that points to register (localhost/index.php?route=Register) but the PHP log indicates that the appropriate Register classes weren't created by the frontController class.

class FrontController
{
    private $controller;
    private $view;

    public function __construct(Router $router)
    {
        $modelName = $router->model;
        $controllerName = $router->controller;
        $viewName = $router->view;

        $model = new $modelName ();
        $this->controller = new $controllerName ( $model );
        $this->view = new $viewName ( $router->getRoute(), $model );

        if (! empty ( $_GET['action'] ))
            $this->controller->{$_GET['action']} ();
    }
    public function output()
    {
        // This allows for some consistent layout generation code
        return $this->view->output ();
    }
}

At this moment I have no idea how to go about solving this issue. And even if I get the classes to be created in the frontController, is there a way to specify that the classes being dynamically generated should extend from a base Model,View,Controller class?
The default HomeView.php looks like this:

class HomeView
{
    private $model;
    private $route;
    private $view_file;


    public function __construct($route, HomeModel $model)
    {
        $this->view_file = "View/" . $route . "/template.php";
        echo $this->view_file;
        $this->route = $route;
        $this->model = $model;
    }

    public function output()
    {
        require($this->view_file);
    }
}  

Any indications on anything that might help me get unstuck or a pointer in the right direction would be much appreciated.

EDIT1:
I forgot to add a summary of my two issues:
1. I would like to understand why the classes aren't being created in the FrontController class...
2. Once created how would I access those classes? Answer is in the comment section. Using the PHP spl_autoload_register function.

Thanks All!

tereško
  • 58,060
  • 25
  • 98
  • 150
iLoveWagons
  • 1,091
  • 4
  • 17
  • 30
  • 1
    Are you doing it all by yourself? Why not use an existing framework instead? – Matteo Tassinari Nov 25 '13 at 19:03
  • 1
    What a verbose way to ask about `spl_autoload_register` and `is_a` functions. Seriously though, why don't you grab one of a zillion already existing PHP MVC frameworks if not to use it then at least to see how things can be architected before you attempt to write your own. – lafor Nov 25 '13 at 19:03
  • @lafor: "architected"? What a verbose way to say "design" `;-)`. – halfer Nov 25 '13 at 19:26
  • I underlined two issues that I'm having. Sorry for not providing a summary at the end. I have provided one now. Thanks for helping with my second issue – iLoveWagons Nov 25 '13 at 19:38
  • I finally understood what I'm supposed to do. Thanks for the help all – iLoveWagons Nov 25 '13 at 20:35

0 Answers0