brtiver blog

mainly about tips of web application tech in English from Japan.

Anonymous controller and trait for Slim3 with PHP7

We can use anonymous classes in PHP7. So I use anonymous classes instead of anonymous functions. Slim3 now supports PSR-7 interfaces for its Request and Response objects. And Slim3 uses extends objects, and use it in your anonymous functions.

But we use Request and Response object directly, so I try to use anonymous classes to write controller action logic and it is to be readability.

code

https://github.com/brtriver/slim3-controller

install

You can install this classes via composer:

composer require brtriver/slim3-controller

Usage

you can read example code.

sample code is blow:

<?php
use Brtriver\Controller\Controller;

$app->get('/hello/{name}', new class($app) extends Controller {

        public function action($args)
        {
            return $this->render($this->container['greet'] . $args['name']);
        }
});

You have to write your controller logic in action method, this action method call when this route is matched. You don't need to use Request and Response objects directly but access with Controller class.

The more you write your routes and similar controller logics with anonymous functions, The more redundancy your application is. and If you define classes with each routes, there are many classes in your application.... then you maybe decide to use full stack frameworks instead of micro frameworks.

So I think it is a one solution to use anonymous classes instead of function.

  • Controller class has these methods and class properties.
    • methods:
      • action($args): you have to write your controller logic in this method.
      • render($output): helper method to render the $output with 200 status
      • render404($output): helper method to render the $output with 404 status
    • properties:
      • $this->request
      • $this->response
      • $this->app
      • $this->container

If use Template engine (PHP or Twig etc...), a renderWithT method in Templatable trait is available:

<?php
use Brtriver\Controller\Controller;
use Brtriver\Controller\Templatable;
$app->get('/hello/{name}', new class($app) extends Controller {
        use Templatable;

        public function action($args)
        {
            return $this->renderWithT('web.html', ['name' => $args['name']]);
        }
});

If use JSON response, a renderWithJson method in JsonResponse trait is available:

<?php
use Brtriver\Controller\Controller;
use Brtriver\Controller\JsonResponse;
$app->get('/json/{name}', new class($app) extends Controller {
        use JsonResponse;

        public function action($args)
        {
            return $this->renderWithJson(['name' => $args['name']]);
        }
});