Fork me on GitHub

Controllers

Used to process requests from the client and solve a number of tasks:

  • Handling errors (app/Controllers/ErrorController.php)
  • Common tasks: change the language, change the color scheme, the license, readme (app/Controllers/IndexController.php)
  • Management blog (app/Controllers/BlogController.php)
  • Registration, Authentication and Authorization users (app/Controllers/SecurityController.php)
  • Task list management (app/Controllers/TodoController.php)
  • Testing the service UBKI (app/Controllers/UbkiController.php)
  • Demonstrating some of the possibilities (app/Controllers/TestController.php)

All these controllers inherit from a generic class BaseController app/Controllers/BaseController.php, which defines common methods for all controllers.


Creating and initializing

Creating and initializing the controllers takes place in the class Bootstrap app/Bootstrap.php using the method _iniControllers.

/**
 *  Initialization controllers
 * 
 * @param Application $app
 */
private function _iniControllers(Application $app) {

    ...

    // Load controllers
    foreach ($app['config']['controllers'] as $controllerServiceName => $controllerClass) {
        $app[$controllerServiceName] = $app->share(function () use ($app, $controllerClass) {
            return new $controllerClass($app);
        });
        $controller = $app[$controllerServiceName];
    }
}

List controllers classes stored in a file app/Resources/Config/parameters.yml.

When you create a controller initialized routes. When a client accesses a certain route to the server, it is called controller and its corresponding action.

public function __construct(Application $app) {

    $this->app = $app;
    $this->iniRoutes();
}

For example, for a controller TodoController routing initialization looks like this:

/**
 * Routes initialization
 * 
 * @return void
 */
protected function iniRoutes() {
    $self = $this;
    $this->app->get('/todo', function () use ($self) {
        return $self->indexAction();
    })->bind('todo');
    $this->app->post('/tasks', function () use ($self) {
        return $self->createAction();
    })->bind('task_create');
    $this->app->get('/tasks/{id}', function ($id) use ($self) {
        return $self->readAction($id);
    })->bind('task_read ');
    $this->app->get('/tasks', function () use ($self) {
        return $self->readAction();
    })->bind('tasks_all ');
    $this->app->put('/tasks/{id}', function ($id) use ($self) {
        return $self->updateAction($id);
    })->bind('task_update');
    $this->app->delete('/tasks/{id}', function ($id) use ($self) {
        return $self->deleteAction($id);
    })->bind('task_delete');
}