[TYPO3-mvc] Standalone Controller

Tobias Liebig tobias.liebig at typo3.org
Fri Sep 14 17:51:02 CEST 2012


Hej,

i think what you want to do (in case of CLI) is an implementation of "Tx_Extbase_MVC_Controller_CommandController".

See an example here (it's not FED related) 
http://fedext.net/features/scheduler-tasks/scheduler-task.html
and here
http://www.q-bic.at/extbase-command-controller/


And In the case of calling extbase from a UserFunc:
http://forge.typo3.org/projects/typo3v4-mvc/wiki/Dispatcher_rework (see the PHP example)




Am 10.09.2012 um 13:47 schrieb Sebastian Michaelsen <sebastian.michaelsen at t3seo.de>:

> 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);
> 		}
> 	}
> 
> }
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc

-- 
Tobias Liebig
TYPO3 Core Team member

TYPO3 .... inspiring people to share!
Get involved: typo3.org

http://bit.ly/supportTYPO3



More information about the TYPO3-project-typo3v4mvc mailing list