The controller

Controller

The controller is the piece of code that respond to the user actions, then modifies the model play around with it and finally call the view to render the screen.

In Tequila the name of your controller is normally passed as a task: task=yourcontroller and the method as an action &action=Browse

    • To respond to a new method simply add a function in the class

The file

So let's create categories controller, to start we add a file called categories.php and we place it on:

app/includes/controllers/categories.php

The code

Now the code, this class should include: browse, view, delete, edit, update, addnew and insert methods (at least), that sounds like a lot of work..

Controller code:

1 <?php 2 class categories extends application_controller_crud { 3 protected $mode = 'browse'; 4 protected $modeltype = 'categories_model'; 5 protected $viewtype = 'categories_view'; 6 protected $idfield = 'I_IDCATEGORY'; 7 protected $no_id_modes = array("Browse", "addNew", "saveAjax","Insert"); 8 } 9 ?>

That's it! Now your controller is ready to respond to all common actions, List records, view, edit, delete, add new and even ajax saving (soon we are adding all methods with XML and JSON replies)

Now you can read more about the controller or jump to the model

What's that code?

    • mode: Defines the default mode if no other specified (i.e. for invoice it might be addNew)

    • model and view type: You can define here the class name, no naming is forced so you can reuse a complex model in many pages

    • idfield: For CRUD pages we normally use the id of the object, other pages can live without this (non autonumeric fields and multiple fields keys are supported, but we don't review in this tutorial)

    • no_id_modes, controller calls are pre-validated to stop execution if no ID is provided (i.e. Read, Update) here we specify which modes should run without validation. If your class contain mostly no_id_modes, you can replace for the opposite $id_modes;

What goes in the controller?

    • Includes all the code that reads post/get/request/session/cookies etc.

    • User events are normally "mapped" to one function in the class

    • Call methods from the model

    • Call methods from the view

What doesn't goes in the controller?

    • DB related code

    • Echo, print, HTML code

    • Language related code or entries

Next - The model