When should the Controller be instantiated?

I am creating an AJAX web application using PHP for my back end. I am trying to create a routing system that will allow me to drop new pages easily and let me focus on Javascript. The actual pages PHP will serve are simple, just views, which are essentially containers for Javascript diagrams (built with d3.js ). This way my controller doesn't even have to interact with my model until I start making AJAX calls.

I'm new to OOP, especially in the end. I do know a bit about Javascript, but I'm brand new to enable OOP with MVC and solve the routing issue. I know there are modules / plugins that have routing classes, but since the back end of this project is very simple - essentially how to best serve the About Me page on a blog - I'd like to take this opportunity to fully explore it ...

I have one controller:

<?php
//controller.php
include 'views/view.php';

class Controller
{

    public function homeAction() {
        $view = new View();
        $view->setTemplate('views/home.php');
        $view->render();
    }

    public function categoryAction($category) {
        $view = new View();
        $view->setTemplate("views/Monitor/{$category}/{$category}.php");
        $view->setCategory($category);
        $view->render();
    }

    public function monitorAction($category, $monitor) {
        $view = new View();
        $view->setTemplate("views/Monitor/{$category}/{$monitor}.php");
        $view->setCategory($category);
        $view->setMonitor($monitor);
        $view->render();
    }

}

?>

      

I am currently instantiating my controller at the beginning index.php

:

<?php
// Load libraries
require_once 'model.php';
require_once 'controller.php';

$controller = new Controller();

$uri = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);

// home action
if ($uri == '/') {
  $controller->homeAction();

// /{category}/{monitor}
} elseif (preg_match("#/(.+)/(.+)#", $uri, $matches) ) {
  $category = $matches[1];
  $monitor  = $matches[2];
  $controller->monitorAction($category, $monitor);

// /{category}
} elseif (preg_match("#/([^/.]+)#", $uri, $matches) ) {
  $category = $matches[1];
  $controller->categoryAction($category);

// 404  
} else {
    header('Status: 404 Not Found');
    echo '<html><body><h1>Page Not Found</h1></body></html>';
}



if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && (!empty($_GET)) && $_GET['action'] == 'get_data') {

    $function = $_GET['chart'] . "_data";
    $dataJSON = call_user_func($function);
    header('Content-type: application/json');
    echo $dataJSON;

}

?>

      

I did a bit of getting around the PHP autoloader, but I want to do it manually first, because I want to be sure and understand the basics.

Is this the right place to create an object Controller

?

+3


source to share


1 answer


First, your architecture is facing some major challenges. You need a router to take care of your requested URIs by users, and then you need an initialization state for your system. The usual way to create Controller

is in the extend

parent class and then in the parent class method __construct

you can initialize your child controllers, however your system is not in good shape.

This is a golden link that I never delete:



http://johnsquibb.com/tutorials/mvc-framework-in-1-hour-part-one

+1


source







All Articles