Controller

The controller is the class that handles the user requests.

Controller flow

    1. Have a function for each call the user can make

    2. Read the Post/Get/Session/Request or config var

    3. Call a method in the model, if required it passes the parameters read in 2, and store the reply in a variable

    4. Calls a method in the view and pass the result of step 3

Adding your own methods:

1 // controller 2 protected function test(&$model, &$view) 3 { 4 $month = safeGet('month', month()); 5 $data = $model->getsample($month); 6 $view->show_month($data); 7 }

To add a custom mode / function, simply declare a function, your function can receive a ready to use model and view.

* If you decide not to use parameters, you can still use the model and the view using $this->model and $this->view

The controller file

In Tequila all controllers need to inherit from one of this 2 classes

    • application_controller

    • application_controller_crud

The 1st one is a normal controller, the one you should inherit from, the second is a controller that includes all methods required for a CRUD page.

Code from a CRUD page

1 <?php 2 class T_RECIPES extends application_controller_crud { 3 protected $mode = 'Browse'; 4 protected $modeltype = 'T_RECIPES_model'; 5 protected $viewtype = 'T_RECIPES_view'; 6 protected $idfield = 'I_IDRECIPE'; 7 } 8 ?>

A normal controller will specify:

$mode : The default mode in case no mode is called

$modeltype: The model class to class to call, this allow models to be exchanged in Tequila

$viewtype: The view class to call, as model

$idfield: Allow Tequila to work with ID's with different names

Useful methods to use in the controller

Safe methods

safePost, safeGet, safeRequest, safeSession

Gets a value from the collection or default;

safePost('myfield', mydefault)

Reading objects

You can retrieve all values of VO in one call using the readpost method, for it to work the names of the fields must match the VO properties

readpost($vo) method,

Reading multiple objects

For multiple objects of the same type you can use:

readpost_serialized($votype)

* This works together with the strategy: view_serialized_objects

Reading multiple JS objects

If you use a MVC model on JS you can use the included JS function JS2PHP to submit them to PHP and then this function to recreate the objects on PHP side

Read more about this methods

I have a problem with checkboxes!

In Update mode when VO's are read from the post, checkboxes sometimes fail to update, this happens when stored value is TRUE and the user change to false, as browsers doesn't submit checkboxes that are unselected.

You can declare a var $checkboxes with an array of the checkboxes in your page to solve this issue, optionally you can add code in your method to solve it.

1 protected $checkboxes = array('I_ACTIVE');

* For other methods related to headers and workflow functions please see application_controller documentation