Categories
MVC PHP

Controller classes

In MVC applications, HTTP requests lead to Controller method invocation (a.k.a. actions). It is inside controller methods that we define the application logic of an MVC application. In other words, this is where we define much of the application flow, user experience, features, etc.

The Controller base class

Given the orchestration role given to controllers, it will be their concern to invoke actions on Models (the data-handling part of the app) and call Views (the app output). We write the Controller base class to enable these methods.

Method model

Assume all model classes are contained in the /app/models folder in files with their class name.php. Furthermore, the program entry point is the index.php file in the document root.

protected function model($model){
  if(file_exists('app/models/' . $model . '.php')){
    require_once 'app/models/' . $model . '.php';
    return new $model();
  }else
    return null;
}

In the above, we check that the file exists and then require it to finally return a new object of this type. If the class definition does not exist, we return null. For the calling method, this will result in an object of the proper type to call the data processing methods.

Method view

The view method is invoked whenever the application is to produce output.

Assume all view files are located in the /app/views folder in files with their name.php. The program entry point is the index.php file in the document root.

protected function view($name, $data = []){
  if(file_exists('app/views/'.$name.'.php')){
    include('app/views/'.$name.'.php');
  } else {
    echo "ERROR: View $view not found!";
  }
}

This method checks the existence of a file and then includes it in order to produce the output defined in this file. If the file does not exist, it simply provides feedback to help the developer fix this error.

The complete Controller class

To complete the class, we simply assemble the two methods and wrap them in a class definition as follows:

class Controller{
  protected function model($model){
    if(file_exists('app/models/' . $model . '.php')){
      require_once 'app/models/' . $model . '.php';
      return new $model();
    }else
      return null;
  }
  
  protected function view($name, $data = []){
    if(file_exists('app/views/'.$name.'.php')){
      include('app/views/'.$name.'.php');
    } else {
      echo "ERROR: View $view not found!";
    }
  }
}

We will now require the Controller base class and extend it in all of the application classes in the /app/controllers folder.

Example Controller

We now explore the code for an example controller that will extend the Controller base class and use the Controller methods to produce useful results.

class HomeController extends Controller{
  public function index(){
    $items = $this->model('item')->get();
    $this->view('home/itemList',['items'=>$items])
  }
}

Given the correct model and view implementations, the above could get all items from a database table and display them in a list output for the user to see and act upon.

Tying it all together

For the controller class to be invoked, it is necessary to make sure that all dependencies are loaded properly and that the program is properly bootstrapped. To take care of the dependencies, we add a file named init.php in the /app folder:

<?php
  require_once 'core/App.php';
  require_once 'core/Controller.php';
?>

and include it from /index.php as in the following:

<?php
  require_once 'app/init.php';
  new App();
?>

With this application completed, the above-defined controller action could be called from a URL such as https://cstutoring.ca/Home/index.

By Michel

My name is Michel Paquette. I currently teach my students how to create data-driven Web applications at Vanier College, in Montreal.

My GitHub page contains a few examples of Web applications. Also consult my YouTube channels: @CSTutoringDotCa and @MichelPaquette.

6 replies on “Controller classes”

In which folder and in which file should this code be located

protected function model($model){
if(file_exists(‘app/models/’ . $model . ‘.php’)){
require_once ‘app/models/’ . $model . ‘.php’;
return new $model();
}else
return null;
}

Please provide the folder structure and what files are in them, as well as the source codes of the files themselves, otherwise nothing is clear

What is it?
protected function view($name, $data = []){
if(file_exists(‘app/views/’.$name.’.php’)){
include(‘app/views/’.$name.’.php’);
} else {
echo “ERROR: View $view not found!”;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.