Controllers

The framework’s controllers are extensions of the Ascmvc\Mvc\Controller class which implements the Ascmvc\AscmvcEventManagerListenerInterface interface. Within the LightMVC Framework, controllers are considered to be the Root Aggregate (main command) of the each and every Event Sourcing aggregate.

Note

For more information on configuring an application’s event sourcing aggregates and the application’s event log, please see the Event Sourcing Configuration section.

Note

For more information on the framework’s event sourcing aggregates in general, please see the Event Sourcing section.

Controller Methods

Every controller has the following basic concrete definition:

class Controller extends AbstractController implements AscmvcEventManagerListenerInterface
{
    public function __construct(array $baseConfig)
    {
        $this->baseConfig = $baseConfig;

        $this->view = $this->baseConfig['view'];
    }

    public static function onBootstrap(AscmvcEvent $event)
    {
    }

    public function onDispatch(AscmvcEvent $event)
    {
    }

    public function onRender(AscmvcEvent $event)
    {
    }

    public function onFinish(AscmvcEvent $event)
    {
    }

    public function indexAction($vars = null)
    {
    }
}

Thus, every controller has an indexAction request handler by default, and every controller has the ability to tap into any of the framework’s major events, except the AscmvcEvent::EVENT_ROUTE event. Upon instantiation of the required controller by the controller manager (dispatcher), a minimal version of the application’s $baseConfig array will be injected into the controller. Upon execution of the controller’s request handler method, all the global server variables are injected into the handler through the $vars variable.

Note

One should avoid as much as possible to use the onBootstrap() method within the controller classes, as this would not scale very well if there is a large number of controllers.

For more information on the event manager and the main MVC events, please see the Event Manager section.

Controller Factories

Any controller can implement the Ascmvc\AscmvcControllerFactoryInterface interface and become a factory that will store a factory of itself in the service manager (Pimple container) and/or return an instance of itself to the controller manager, after completing some specific logic.

This is useful if you need to set up some specific service or resource before injecting it into an instance of the controller.

Note

It is not recommended to inject the entire application object or the service manager into the controller, but to only inject the services that you actually need in order to respond to the request.

For a working example, please see the section on the LightMVC Skeleton Application.

For information on how to deal with other types of factories, please see the Service Manager section.