Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

...

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

...

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')

...

  • 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 manipulationmethod 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 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 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 pertains to backend views for returns a KenedoView for showing a list of KenedoModel data records, see upcoming articles on how to use those. Use NULL for our examplerecords for backend use. See Creating a backend list and edit view.

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

Code Block
languagephp
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;
	}

}

...

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.

...

  • 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.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

...