[TYPO3-mvc] Standalone Controller
Sebastian Michaelsen
sebastian.michaelsen at t3seo.de
Mon Sep 10 13:47:05 CEST 2012
Hey *,
from time to time I came accross situations where I wanted to use
Controllers, Actions, Persistence etc NOT in a context of rendering a
Frontend-Plugin or Backend-Module.
But I found it hard to invoke Extbase within a UserFunc or through a
request coming from CLI.
I there a good standard way to do this?
For my usage I implemented a StandaloneController that can be extended
by any controller and that provides some basic Extbase functionality:
- Call an Action (no Argument validation or mapping)
- Render the view implicitly
- Take care of persistence
Usage:
$controller =
$objectManager->get('Me\\MyProject\\Controller\\MyController'); //
MyController extends the StandaloneController
$controller->setTemplatePath('somePath/to/fluidTemplates/');
return $controller->callAction('index');
So if there is no good standard way to do this and if anyone else needs
such functionality, here's my controller (tested with 6.0 master):
namespace Me\MyProject\Controller\Generic;
abstract class StandaloneController {
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\Manager
* @inject
*/
protected $persistenceManager;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @inject
*/
protected $objectManager;
/**
* @var \TYPO3\CMS\Fluid\View\StandaloneView
* @inject
*/
protected $view;
/**
* @var string
*/
protected $templatePath;
/**
* @var string
*/
protected $format = 'html';
/**
* @param string $format
*/
public function setFormat($format) {
$this->format = $format;
}
/**
* @param $path
*/
public function setTemplatePath($path) {
$this->templatePath = rtrim($path, '/') . '/';
}
/**
* @param string $action
* @return string
* @throws \Exception
*/
public function callAction($action) {
$arguments = func_get_args();
// remove first argument, it's already assigned to $action
array_shift($arguments);
if(method_exists($this, $action . 'Action')) {
$this->view->setFormat($this->format);
$this->view->setTemplatePathAndFilename($this->templatePath . $action
. '.' . $this->format);
$content = call_user_func_array(array($this, $action . 'Action'),
$arguments);
if(!isset($content)) {
$content = $this->view->render();
}
$this->persistenceManager->persistAll();
return $content;
} else {
throw new \Exception('Action does not exist', 1347054560);
}
}
}
More information about the TYPO3-project-typo3v4mvc
mailing list