Controllers define a set of one or more related actions.
Each controller extends the base Controller class defined in /core/Controller.class.php. Controllers should be named related to the functionality of its actions. The naming convention for controllers differs from normal PHP naming conventions. Controller names should be all lowercase and suffixed with _controller (ex: users_controller). The name of the controller is used by the core to map file includes (ex: the path http://example.com/users/index maps to the controller users_controller). Controller files should be stored in the /app/controllers directory and named [controller name].controller.php (ex: /app/controllers/users.controller.php).
The constructor function is optional with controllers. Constructors should be defined for uses such as including models that are used in every action.
NOTE: Controllers should only contain functions that define actions. Other functions that are used by the actions in the controller should be contained in a helper file for the controller. If there are functions that actions in multiple controllers use, those functions should be placed in the /app/helpers/application.helper.php file. For more information, read the Helpers Overview page.
Example of a controller that does not use a constructor:
class cars_controller extends Controller { /** * View action details */ public function view() { // view action contents } /** * Buy action details */ public function buy() { // buy action contents } /** * Sell action details */ public function sell() { // sell action contents } }
Example of a controller that has a constructor:
class users_controller extends Controller { /** * Constructor details */ public function __construct() { // include required models for all/most actions Fabriq::model('Users'); // rest of constructor contents contents } /** * Log in action details */ public function login() { // log in action contents } /** * Log out action details */ public function logout() { // log out action contents } }
