Flow3 - How to initialize and use a global Controller?

506 Views Asked by At

I've now some entities, reposetories and controllers in flow3 and they work very well. In case someone needs a global fluid variable (e.g. a username printed in the default layout) he has to define and use a global controller.

How can I create and use such controllers?

1

There are 1 best solutions below

0
On

Controllers are classes, so you can extend them and create abstract ones.

Let's assume you have LoveController in My.Relationships package. You can create some separate base package i.e. My.Life with AbstractLifeController inside (php: abstract class AbstractLifeController extends \TYPO3\Flow\Mvc\Controller\ActionController).

Now there are initialize* methods (where * might be Action, View, AnyAction and so on). In your case use: protected initializeView(\TYPO3\Flow\Mvc\View\ViewInterface $view) in this abstarct controller and there assign some variables i.e. $view->assign('myName', $myName); ...

Then when your LoveController extends \My\Life\Controller\AbstractLifeController, that variable {myName} will be visible in fluid for templates used by its actions.

So instead of extending \TYPO3\Flow\Mvc\Controller\ActionController for all your controllers, just extend this abstract one \My\Life\Controller\AbstractLifeController