By Sebastiaan de Jonge, published on Monday, June 10, 2013 at 22:40

In this post I will give a short demonstration on how to extend existing classes within Extbase. For example to extend another extension's or plugin's functionality. With piBase extensions we could do so by using the XCLASS construction, this post will demonstrate how we do this in Extbase.
In order to demonstrate this principle properly, I have set up a very simple but pointless plugin. To make testing easier, I have added the extension within same extension. Of course, in a normal situation this would be pointless since we could modify the controller directly. However, this is an easier way to offer a working example without having to download and install multiple extensions.

Example

In this tiny extension there is one main controller (ExampleController), and of course an extension of that controller (OtherExampleController). The action we are using is called randomAction and will be generating a random MD5 hash. In the extended controller it will add a random SHA1 hash as well. Let's have a quick look at booth controllers.
class Tx_SdjEcee_Controller_ExampleController extends Tx_Extbase_MVC_Controller_ActionController {
	/**
	 * Random action, created a random MD5 hash
	 *
	 * @return void
	 */
	public function randomAction() {
		$this->view->assign('randomMd5', md5(microtime()));
	}
}
class Tx_SdjEcee_Controller_OtherExampleController extends Tx_SdjEcee_Controller_ExampleController {
	/**
	 * Random action, created a random SHA1 hash
	 *
	 * @return void
	 */
	public function randomAction() {
		parent::randomAction();
		$this->view->assign('randomSha1', sha1(microtime()));
	}
}
The first thing we need to pay attention to, is that our extending controller (OtherExampleController) actually extends the original controller class.
class Tx_SdjEcee_Controller_OtherExampleController extends Tx_SdjEcee_Controller_ExampleController
In this case I want to preserve the functionality of the original controller, so I'm calling the parent function to do so.
parent::randomAction();
The final thing we need to do to make it all work is tell Extbase we are actually extending our original class. This is done using the TypoScript setup.
# Extend Example Controller
config.tx_extbase {
	objects {
		Tx_SdjEcee_Controller_ExampleController {
			className = Tx_SdjEcee_Controller_OtherExampleController
		}
	}
}
All set! You can use the example included in this post to see the result. Don't forget to include the static template, otherwise the extended functionality will not be loaded (which is also a good way to demonstrate it's function).If you have any questions or remarks, feel free to leave a comment.
Benjamin Serfhos
Benjamin Serfhos - Tuesday, June 11, 2013 at 11:56

Nice post! Simple adjustment for an easy example! 🙂

Fedir
Fedir - Tuesday, November 5, 2013 at 17:01

Liked a lot the way it works.

mtness
mtness - Monday, January 4, 2016 at 16:24

I am overlooking the config.tx_extbase part too often!