2017 © Pedro Peláez
 

library slim3-controller

Anonymous controller and trait class for Slim3

image

brtriver/slim3-controller

Anonymous controller and trait class for Slim3

  • Saturday, January 2, 2016
  • by brtriver
  • Repository
  • 2 Watchers
  • 4 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Slim3-controller

Build Status, (*1)

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., (*2)

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. read more, (*3)

Requirements

  • PHP 7.0 or later.
  • Slim3

Install

install via composer, (*4)

composer require brtriver/slim3-controller

Usage

you can read example code., (*5)

sample code is blow:, (*6)

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']);
        }
});
  • Controller class has these methods and class properties.
    • methods:
    • action($args): you should 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:, (*7)

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:, (*8)

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']]);
        }
});

License

slim3-controller is licensed under the MIT license., (*9)

The Versions

02/01 2016

dev-master

9999999-dev

Anonymous controller and trait class for Slim3

  Sources   Download

MIT

The Requires

 

by Avatar brtriver

01/01 2016

v0.1

0.1.0.0

Anymous controller class for Slim3

  Sources   Download

MIT

The Requires

 

by Avatar brtriver