Skip to content

How To Use Controllers

Youmy001 edited this page Feb 4, 2018 · 4 revisions

Controllers are the end point of every requests. They manipulate data inputs and update model states before returning a view filled with processed data. For more documentation about Controllers, check the section about the MVC module (Coming Soon).

Controllers must be located in the controllers directory in order for the router to find it and to execute its code. Each controller files must contain only one controller class. A controller class must inherit from \Apine\MVC\Controller. Controllers are part of the namespace \Apine\Controllers\User.

Naming convention

  • A controller class name is made of a name followed by "Controller" in a PascalCased fashion;
  • The file name must be the same as the class name.

Per example, if you are writting a controller for user management you want to name "user". The class name would be UserController then the file name would be UserController.php.

Example

namespace Apine\Controllers\User;

use Apine\MVC\Controller

class HomeController extends Controller{

    public function index(){
        
    }
}

Actions

An action is a method in a controller class. Its designed to take an array as an optional argument. Its name is used by the router with the controller name to find and execute the action.

How to call an Action

Controllers and their actions are by default accessible via controller/action. Then you can append any number of parameter to the string. It is possible to modify this behavior with Routes.

Inputs

In order to access to arguments from the query string or form data, you must specify one argument to you action method. It is an aggregate of the $_GET, $_POST and $_FILES super arrays previously passed through some pre-validation processes.

GET arguments are accessible with numeric indexes. POST and FILES arguments are accessible with their field name as index just like in $_POST and $_FILES arrays.

Example

function about () {
    // Do stuff here
}

function page ($params) {
    $page_no = $params[0]; // First get arguments in URI
    $username = $params[‘username’]; // A POST argument
}

Return value

An action should return a view that will be then processed and sent back to the client. There are two main types of views : HTML views and JSON views.

Example

public empty_action () {
    $view = new Apine\MVC\HTMLView();
    return $view;
}