Controllers

Controllers are where your request-response cycles begin and end. A route is matched to a controller, and a method on that controller, and dispatched through. Controllers in Flint are actually all Controller Services, a Silex and Symfony concept where each controller is defined as a regular service, with some special magic (specifically, appending .controller to the service name, and making it share()-ed so it’s a singleton). See the usage documentation at Silex’s documentation site, and how to define them in Flint below.

Syntax For app/controllers.php

1
2
3
4
5
6
7
8
<?php
use FlintExample\Controller\ExampleController;

return [
    'example' => function() {
        return new ExampleController();
    }
];

This config file shows how a controller is defined, named example, which will be accessed in the routes by example.controller. It uses a callback to define the controller: this will allow you to bring in services easily.

Injecting A Service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
use FlintExample\ExampleApp,
    FlintExample\Controller\ExampleController;

return [
    'example' => function() {
        // Retrieve the service named 'Hello' from the DI container
        $hello = ExampleApp::getInstance()['Hello'];

        // Inject it into the controller
        return new ExampleController($hello);
    }
];