Controllers

Last updated: Wed, 12 Sep 2018 09:48

It's classes whose control interactions between models, services and templates. The controller is instanced and the matched method is called, by router service when the application start.

Class

\Berlioz\HttpCore\Controller\AbstractController

It's the main controller for website projects who offer methods for services, routing, templating, flash messages, and redirection.

Magic methods

Two methods are reserved for system:

  • _b_pre(): method called before execution of controller method
  • _b_post(): method called after execution of controller method

Example

class MyController extends \Berlioz\HttpCore\Controller\AbstractController
{
    /**
     * Method description.
     *
     * @param \Berlioz\Http\Message\ServerRequest $request
     * @param \Berlioz\Http\Message\Response $response
     *
     * @return \Berlioz\Http\Message\Response $response
     * @route( "/my-route/{attr1}" )
     */
    public function myMethod(ServerRequest $request, Response $response): Response
    {
        // Do something
        $attribute = $request->getAttribute('attr1');

        return $response;
    }
}

Parameters

Parameters given to the methods are injected automatically by the Instantiator class of service container.

Attributes of routes are available with ServerRequest parameter, with method getAttributes().