KenedoController

What is this article for

In this article you learn how to leverage controllers for executing tasks like displaying content and triggering processes for custom application logic.

Be sure to have learned how to create a backend list and edit form in this article before proceeding.

Concept

All browser HTTP requests go through a controller. Each controller has a number of tasks. When it comes to displaying content, the difference to the traditional MVC pattern is that views are responsible for loading data from the model.

Base facts

  • Controllers are a single PHP file with a specific file name and a class name

  • All extend KenedoController

  • Each controller has a 'name', for example 'example', file names and PHP class names derive from it

  • There is a directory for built-in controllers and a directory for custom controllers (see File locations)

  • Task names (like 'display', 'store' etc) relate to controller method names.

  • Each controller class has 4 abstract functions that you implement. You override other methods as you see fit

Creating a custom controller

First you create a PHP file holding the controllers PHP class. Location of custom controller file: {customization folder}/controllers/{controller name}.php (in our example 'example.php')

The name of the PHP class is ConfigboxController{controller name}, in our example ConfigboxControllerExample.

It extends KenedoController.

class ConfigboxControllerExample extends KenedoController { }

In the class you implement 4 abstract methods:

  • function getDefaultModel(): This method returns the KenedoModel responsible for CRUD operations. The controllers base methods uses the model’s methods. Return null if your controller works without a model.

  • function getDefaultView(): This method returns the KenedoView responsible for rendering browser output. The controller’s display method will use this view for rendering content. Return NULL in case there is no output (e.g. only JSON output).

  • function getDefaultViewList(): This returns a KenedoView for showing a list of records for backend use. See Creating a backend list and edit view.

  • function getDefaultViewForm():  Just as above, but for edit forms. See Creating a backend list and edit view.

 

class ConfigboxControllerExample extends KenedoController { /** * @return NULL */ protected function getDefaultModel() { return NULL; } /** * @return NULL */ protected function getDefaultView() { return NULL; } /** * @return NULL */ protected function getDefaultViewList() { return NULL; } /** * @return NULL */ protected function getDefaultViewForm() { return NULL; } }

 

As you can see, in this example our controller doesn't do much. Let's have our controller output the HTML of a KenedoView. For this refer to the KenedoView article, so you know how to create a view and then circle back here to see how it integrates with controllers.

From here on you'll have a KenedoView named example in your customization folder. Let's link it up with the controller - we change the controller method getDefaultView():

// This will replace the getDefaultView() method in your example controller. /** * @return ConfigboxViewExample */ protected function getDefaultView() { return KenedoView::getView('ConfigboxViewExample'); }

The curious of you will wonder how the return value of this method relates to displaying the view's content when you load a HTML page. See the display() method in KenedoController. It takes in the getDefaultView()'s object and (among other things) calls the view's display() method.

Putting the controller to use

At this point you may want to see things doing some action. So how do we get the controller to output the view's content?

Joomla and Wordpress

When it comes to outputting your view's content, you'd typically use a menu item (see the KenedoView article on 'On Joomla using a menu item'). For understanding's sake you see now how you call a controller task via URL routing in Joomla.

Simply use this URL to get the controller to do his task:

  • This is for our example for Joomla outputting the view within the active Joomla template (or Wordpress theme): index.php?option=com_configbox&controller=example&task=display

  • If you want the output to render without the CMS’s template/theme, add the GET param output_mode=view_only

Tasks other than displaying views

For any functionality you want to build, you add a method to your controller file and via a specific URL, Wordpress or Joomla will route to your method and call it. Whatever the method's call puts into the PHP output buffer will be outputted into the page's content.

Example:

  1. Say you want to use a custom task in your example controller and execute the task via web browser or HTTP client. 

  2. Let's call the task 'doGreatThings' and have it output 'I did great things'

 

Give it a try:

index.php?option=com_configbox&controller=example&task=doGreatThings

Useful KenedoController methods

KendoController::setRedirect($url)

Supply the URL (full URL) to redirect a user to it.

KenedoController::isAuthorized($task = '')

All built in tasks use this method to see if the current user is allowed to execute said tasks. By default on Joomla it looks for core.manage rights on CB and on Wordpress sees if the user can edit pages or not. Override the methods to your needs.

KRequest methods

Use these methods to get sanitized GET or POST data. See the class' PHPDoc documentation for details.

KenedoPlatform::p()→getDb()

Gets you CB's MySQL database object for doing CB queries. See usages in the CB codebase for details.