Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

The DataController

Eduardo Diaz edited this page Jun 20, 2018 · 3 revisions

If you have not read the Controller documentation please do so now.

As we have already explained, the DataController is the boss of a data-aware view. Its job is to get one or more Model object instances, a View object instance, assign the Model object instances to the View object instance and ask the View object to render itself.

Controller vs DataController

Since this is a data-aware controller the Model object instances it expects to create are instances of FOF30\Model\DataModel or its children, e.g. FOF30\Model\TreeModel (data model for nested sets). The View it creates is an object instance of a class implementing the FOF30\View\DataView\DataViewInterface interface. The DataController will try to automatically figure out which class to use based on the format of the request and the availability of an XML form file for this view.

One Controller for singular and plural view

FOF uses the Ruby on Rails convention for singular and plural named data-aware views. It considers the views "item" and "items" to be the same as far as the controller is concerned: both views will use the controller Item, i.e. the singular name of the view with its first letter in uppercase.

If a task is not provided in the request, FOF will call DataController's getCrudTask() method to figure out which task to use. The default behaviour is:

  • If the View is plural we return the "browse" task.
  • If it's a POST or PUT HTTP request we return the "save" task.
  • If it's a DELETE HTTP request we return the "delete" task.
  • If the View is singular, a non-empty id parameter is present in the request and the user IS NOT logged in we return the "read" task.
  • If the View is singular, a non-empty id parameter is present in the request and the user IS logged in we return the "edit" task.
  • If the View is singular, an empty or no id parameter is present in the request and the user IS logged in we return the "add" task.

If you want to change this behaviour override the getCrudTask() method in your class.

If you want to force a different task than the one specified in the request you need to override the execute() method in your class.

Class and file naming conventions

The controller class files are found inside your component's Controller directory. The controllers should have a name which is a word in singular, e.g. "Item" (not "Items"). This is mandatory for data-aware controllers.

Like all FOF classes, the Controller class' name is the same as the filename and the namespace depends on what is the namespace of your component and which section (frontend, backend) you're talking about.

Considering a component com_example with namespace prefix Acme\Example and a backend Controller named Item, the file administrator/components/com_example/Controller/Item.php has the following contents:

<?php

namespace Acme\Example\Admin\Controller;

class Item extends FOF30\Controller\DataController
{
    // Controller implementation goes here
}

Customising your Controller

Unlike plain old Joomla! you are NOT supposed to copy and paste code when dealing with FOF. Our rule of thumb is that if you ever find yourself copying code from DataController into your extension's specialised controller class you're doing it wrong.

Do remember that FOF will automatically trigger onBefore and onAfter events before and after calling your task method. For example the events onBeforeSave and onAfterSave will be triggered before and after the execution of the save task. You can handle them in this and children classes by creating protected methods, e.g.

protected function onBeforeSave()
{
    // Do something here...
}

This is how you should be customising your DataControllers: by handling the onBefore/onAfter events triggered by FOF. Do not override the task methods themselves.

Also note that these events can also be handled by Joomla! plugins. Please read the page on using Joomla! plugins for FOF events.

Clone this wiki locally