[TYPO3-mvc] how to make a controller extension friendly

Jochen Rau jochen.rau at typoplanet.de
Wed Apr 7 12:07:48 CEST 2010


Hi Masi.

On 06.04.10 21:56, Martin Kutschker wrote:
> While playing with extending extensions with another I noticed that you have to rewrite mayn
> functions just to get the right class (for the extending extension).
>
> Here's a way to make extending a controller that works on a repository easy to extend. If the other
> extensions uses the same directory layout (and it should) then the extending controller will get the
> expected repository (the one belonging to the extending extension).
>
> class Tx_BallroomDancing_Controller_DanceController extends Tx_Extbase_MVC_Controller_ActionController {
>
> 	/**
> 	 * Pattern to build a dance repository.
> 	 * @var string
> 	 */
> 	protected $danceRepositoryNamePattern = 'Tx_ at extension_Domain_Repository_DanceRepository';
>
> 	/**
> 	 * @var Tx_BallroomDancing_Domain_Repository_DanceRepository
> 	 */
> 	protected $danceRepository;
>
> 	/**
> 	 * Initializes the current action
> 	 *
> 	 * @return void
> 	 */
> 	public function initializeAction() {
> 		// be extension friendly
> 		$danceRepositoryName = str_replace('@extension',
> 			$this->request->getControllerExtensionName(),
> 			$this->danceRepositoryNamePattern
> 		);
> 		$this->danceRepository = t3lib_div::makeInstance($danceRepositoryName);
> 	}
> }

This is related to the thread "[TYPO3-mvc] "= new" vs 
"t3lib_div::makeInstance()" in Extbase".

Your solution will work but forces the developer to add a lot of 
specific code to the controller. IMO is the wrong place to do this (as 
XCLASS is, too ;-) IMHO). In FLOW3 we have:

class PostController extends \F3\Blog\Controller\AbstractBaseController {

	/**
	 * @inject
	 * @var \F3\Blog\Domain\Repository\PostRepository
	 */
	protected $postRepository;

}

and the framework is responsible to inject the right implementation 
(forced to use a specific class in this case). To make this flexible you 
can use (in FLOW3):


class PostController extends \F3\Blog\Controller\AbstractBaseController {

	/**
	 * @inject
	 * @var \F3\Blog\Domain\Repository\PostRepositoryInterface
	 */
	protected $postRepository;

}

Then, an instance of \F3\MyPackage\Domain\Repository\PostRepository 
(implementing the above interface) can be injected. This is a clean OOP 
solution.

In Extbase we haven't got an object management to resolve this, yet. My 
proposed solution is to alter the makeInstance method to deliver the 
correct implementation based on convention or configuration.

Regards
Jochen


More information about the TYPO3-project-typo3v4mvc mailing list