Vesthelm Engine

2.1.4 User Guide

Controller Class

The Controller class controls the Model class, the View class, and any other resources needed to process the HTTP request and generates a web page.

What is a Controller?

A Controller is simply a class file or a file that is named in a way that can be associated with a URI:

http://www.example.com/index.php/bulletin-board/add-bulletin/

In the above example, Vesthelm Engine would attempt to find a controller named Add_Bulletin.php in applications/bulletin_board/frontend/controllers directory and run it.

Also controller can be a file and Vesthelm Engine would attempt to find a controller file named add-bulletin.php in applications/bulletin_board/frontend/files directory.

Creating a Controller

Let's say you want to create bulletin board application. You might have a controller class that allows you to display add bulletin page. Here is an example of what such a controller class might look like:

defined('V_IN_FRONTEND') or die('Powered by <a href="http://www.vesthelm.com/">Vesthelm EE</a>');

class Bulletin_Board_Add_Bulletin_Controller extends Vesthelm_Controller {

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

    public function output() {
        $this->View->assign('title', 'Add Bulletin Page');

        $this->View->display('add-bulletin');
    }

}

execute() function always be executed when running controller: $this->bulletin_board->Bulletin_Controller->run()

Anatomy of a Controller

Controller classes are stored in folders:

//Core controllers
example.com/frontend/controllers
example.com/acp/controllers

//Application controllers
example.com/applications/{application_name}/frontend/controllers
example.com/applications/{application_name}/acp/controllers

//Addon controllers
example.com/addons/{addon_name}/frontend/controllers
example.com/addons/{addon_name}/acp/controllers

//Sometimes controllers can be found here (old way):
example.com/addons/{addon_name}/frontend/files
example.com/addons/{addon_name}/acp/files

The basic prototype for controller class is:

class Enhancement_Name_Controller_Name_Controller extends Vesthelm_Controller {
    public function execute() {}
}

Where Enhancement_Name is the name of your enhancement. Enhancement names must have the first letter capitalized after underscore _ character.

Where Controller_Name is the name of your class. Class names must have the first letter capitalized after underscore _ character. Make sure your class extends the base Controller class.

The file name will be same as your class name (Controller_Name). For example, if your class is:

class Bulletin_Board_Add_Bulletin_Controller extends Vesthelm_Controller {
    public function execute() {}
}

Your file will be:

example.com/applications/bulletin-board/acp/controllers/Add_Bulletin.php

//or
example.com/applications/bulletin-board/acp/controllers/add-bulletin/Add_Bulletin.php

Calling a Controller

$Add_Bulletin_Controller = $this->bulletin_board->Add_Bulletin_Controller;

Where bulletin_board is the name of your enhancement and Add_Bulletin is the name of your controller.

Running a Controller

To manually load a controller:

$this->bulletin_board->Add_Bulletin_Controller->run();

Where bulletin_board is the name of your enhancement and Add_Bulletin is the name of your controller.

What is a Controller Action?

A Controller Action is simply a class file or a file that is named in a way that can be associated with a URI:

http://www.example.com/index.php/bulletin-board/bulletin/add

More URI examples: Getting an action name

In the above example, Vesthelm Engine would attempt to find a controller action file named Bulletin_Add.php in applications/bulletin_board/frontend/controllers/bulletin/ directory and run it.

Also a controller action can be a file and Vesthelm Engine would attempt to find a controller action file named add.php in applications/bulletin_board/frontend/files/actions/bulletin/post/ directory.

Creating a Controller Action

At first we need to create Bulletin Action_Model class that will contain all controller actions and fields same as: Creating Action_Model

Controller:

defined('V_IN_FRONTEND') or die('Powered by <a href="http://www.vesthelm.com/">Vesthelm EE</a>');

class Bulletin_Board_Bulletin_Controller extends Vesthelm_Controller {

    public function execute() {
        $this->control(); //control actions... default action - run output() method
    }

    public function output() {
        $this->View->assign('title', 'Add Bulletin Page');

        $this->View->display('add-bulletin');
    }

}

control() function used to control controller actions, if no actions will be found controller will run output() function.

Controller Action:

defined('V_IN_FRONTEND') or die('Powered by <a href="http://www.vesthelm.com/">Vesthelm EE</a>');

class Bulletin_Board_Bulletin_Add_Controller extends Vesthelm_Controller {

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

    public function output() {
        $this->Output->json(array('key' => '123'));
    }

}