TYPO3: Sane extbase controller names

Extbase, TYPO3's new standard framework for writing extensions, has a bunch of characteristics a sane man can not get accustomed to. Today's example is the class names of controllers.

A controller's name is determined by the following scheme:

$extension\Controller\SomeNameController

That's Controller\FooController. ControllerController. controllercontrollercontrollercontroller.

Fix it

Since extbase is a mature framework, there is probably a way to fix this. And there is! Easy as eating a pie.

5. Request

The controller name schema is defined in \TYPO3\CMS\Extbase\Mvc\Web\Request, property $namespacedControllerObjectNamePattern. Since there is no way to inject that property, extend the Request class and overwrite the pattern:

<?php
namespace my\extension;
 
class ExtBase_Request
    extends \TYPO3\CMS\Extbase\Mvc\Web\Request
{
    /**
     * Pattern after which the namespaced controller object name is built
     *
     * @var string
     */
    protected $namespacedControllerObjectNamePattern
        = '@extension\Controller_@controller';
}
?>

4. ObjectManager

Request objects are created in \TYPO3\CMS\Extbase\Object\ObjectManager::get(), so we need to overwrite this method as well:

<?php
namespace my\extension;
 
class ExtBase_ObjectManager
    extends \TYPO3\CMS\Extbase\Object\ObjectManager
{
    public function get($objectName)
    {
        $arguments = func_get_args();
        if ($objectName == 'TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request') {
            $arguments[0] = '\\my\\extension\\ExtBase_Request';
        }
        return call_user_func_array(array('parent', 'get'), $arguments);
    }
}
?>

3. Web\RequestBuilder

The object manager is instantiated in \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder, again without an official way to override it. Let's extend the class just to change a line in the docblock:

<?php
namespace my\extension;
 
class ExtBase_WebRequestBuilder
    extends \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
{
    /**
     * @var \my\extension\ExtBase_ObjectManager
     * @inject
     */
    protected $objectManager;
}

2. FrontendRequestHandler

Our web request builder comes from an @var annotation in \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler. Another extended class:

<?php
namespace my\extension;
 
class ExtBase_FrontendRequestHandler
    extends \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler
{
    /**
     * @var \my\extension\ExtBase_WebRequestBuilder
     * @inject
     */
    protected $requestBuilder;
 
    public function getPriority()
    {
        //more than the parent one
        return 200;
    }
}
?>

1. TypoScript

The frontend request handler class can finally be configured via TypoScript, without extending another class:

plugin.tx_myextension {
    mvc.requestHandlers {
        my\extension\ExtBase_FrontendRequestHandler = my\extension\ExtBase_FrontendRequestHandler
    }
}

We're done! In 5 easy steps we got proper controller class names that don't drive us mad when thinking of them.

Written by Christian Weiske.

Comments? Please send an e-mail.