[TYPO3-mvc] Change view in Controller via TS

Michael Knoll mimi at kaktusteam.de
Mon Aug 9 18:06:38 CEST 2010


Hallo list,

I had the problem of changing a view depending on a TS setting. So what 
I did was extending the ActionController and overwriting the resolveView 
method. This led to the following code:


     protected function resolveView() {
     	// These lines have been added by Michael Knoll to make view 
configurable via TS
         $viewClassName = 
$this->settings['controller'][$this->request->getControllerName()][$this->request->getControllerActionName()]['view'];
         if ($viewClassName != '') {
         	if (class_exists($viewClassName)) {
         		$view = $this->objectManager->getObject($viewClassName);
         	} else {
         		throw new Exception('View class does not exist! ' . 
$viewClassName);
         	}
         } else {
             $view = 
$this->objectManager->getObject('Tx_Fluid_View_TemplateView');
         }

         $controllerContext = $this->buildControllerContext();
         $view->setControllerContext($controllerContext);

         // Template Path Override
         $extbaseFrameworkConfiguration = 
Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
         if 
(isset($extbaseFrameworkConfiguration['view']['templateRootPath']) && 
strlen($extbaseFrameworkConfiguration['view']['templateRootPath']) > 0) {
 
$view->setTemplateRootPath(t3lib_div::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']));
         }
         if 
(isset($extbaseFrameworkConfiguration['view']['layoutRootPath']) && 
strlen($extbaseFrameworkConfiguration['view']['layoutRootPath']) > 0) {
 
$view->setLayoutRootPath(t3lib_div::getFileAbsFileName($extbaseFrameworkConfiguration['view']['layoutRootPath']));
         }
         if 
(isset($extbaseFrameworkConfiguration['view']['partialRootPath']) && 
strlen($extbaseFrameworkConfiguration['view']['partialRootPath']) > 0) {
 
$view->setPartialRootPath(t3lib_div::getFileAbsFileName($extbaseFrameworkConfiguration['view']['partialRootPath']));
         }

         if ($view->hasTemplate() === FALSE) {
             $viewObjectName = $this->resolveViewObjectName();
             if (class_exists($viewObjectName) === FALSE) 
$viewObjectName = 'Tx_Extbase_MVC_View_EmptyView';
             $view = $this->objectManager->getObject($viewObjectName);
             $view->setControllerContext($controllerContext);
         }
         if (method_exists($view, 'injectSettings')) {
             $view->injectSettings($this->settings);
         }
         $view->initializeView(); // In FLOW3, solved through Object 
Lifecycle methods, we need to call it explicitely
         $view->assign('settings', $this->settings); // same with 
settings injection.
         print_r(get_class($view).'<br>');
         return $view;
     }



Only the first view lines are interesting. Perhaps you guys could build 
that into extbase, as it's an often requested feature...


Greez

Mimi


More information about the TYPO3-project-typo3v4mvc mailing list