php - When should the Controller get instantiated?


I am building an AJAX web app, using PHP for my back end. I am trying to design a routing system that will let me easily drop new pages in, and let me focus on the Javascript. The actual pages that PHP will be serving up are simple, just views that are essentially containers for Javascript charts (built with d3.js). Thus, my controller won't even have to interact with my model until I start making AJAX calls.

I am new to OOP, especially in back end. I've been doing a bit with Javascript, but I am brand new to incorporating OOP with MVC & solving the issue of routing. I know there are modules/plugins out there that have Routing classes written, but as the back end part of this project is very straight-forward - essentially, how best to serve up an 'About' page on a blog - I'd like to take this opportunity to learn it thoroughly myself.

I have one controller:

include 'views/view.php';

class Controller

    public function homeAction() {
        $view = new View();

    public function categoryAction($category) {
        $view = new View();

    public function monitorAction($category, $monitor) {
        $view = new View();



Right now, I instantiate my controller at the beginning ofindex.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 == '/') {

// /{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];

// 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 have read a bit about PHP's autoloader, but I'd like to get it down manually first, because I want to make sure and understand the fundamentals.

Is this the appropriate place to instantiate myController object?




First, your architecture is facing some major problems. You need a router to take care of your requested URIs by the users and next you need an initialization state for your system. The usual way to createControllers is toextend a parent class, then in your parent class__construct method you can initialize your children controllers, however, your system isn't in a good shape.

This is a gold link that I never delete:

People are also looking for solutions to the problem: php - How do I get next occurrence of a certain day of the month


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.