Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

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.

This article assumes you have a solid understanding of the MVC development pattern. Refer to literature or good resources on the web to make good use of this article.

Concept

KenedoController classes are the base class for MVC controllers and come with class methods that handle functionality like authorization, CRUD storing (in conjunction with KenedoModels and KenedoViews).

What makes KenedoControllers different from typical MVC patterns:

  • For rendering content for pages, controllers do not handle data loading. Instead, KenedoViews have a dedicated function prepareTemplateVars() that deals with that.

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 returns NULL if the controller doesn't need any model or a KenedoModel (or any object really) that deals with data loading or manipulation.

  • function getDefaultView(): This returns NULL in case the controller doesn't deal with views or the KenedoView you want to use. You get your KenedoView object using the factory method KenedoView::getView(), supplying the class name of your view as parameter.

  • function getDefaultViewList(): This pertains to backend views for a list of KenedoModel data records, see upcoming articles on how to use those. Use NULL for our example

  • function getDefaultViewForm():  Just as above, but for edit forms to KenedoModel data records.

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

  • You can use Joomla's tmpl query string parameter to output the view with your template's component template by adding the query string key/value pair tmpl=component

  • On Wordpress, we replicated Joomla's tmpl query string parameter behavior, so using it on WP gets you the same effect.

  • For absolutely raw output of the view only, add the key/value query string pair format=raw.

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'

class ConfigboxControllerExample extends KenedoController {


	// Mind your 4 functions need to be there too
	
	function doGreatThings() {
		echo '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.

  • No labels