PHP code example of ellipse / handlers-controller

1. Go to this page and download the library: Download ellipse/handlers-controller library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ellipse / handlers-controller example snippets




namespace App\Controllers;

use Psr\Http\Message\ServerRequestInterface;

use App\SomeService;
use App\SomeOtherService;

class SomeController
{
    public function __construct(SomeService $service)
    {
        //
    }

    public function index(SomeOtherService $service)
    {
        // return a Psr-7 response
    }

    public function show(SomeOtherService $service, $some_id)
    {
        // return a Psr-7 response
    }

    public function store(ServerRequestInterface $request)
    {
        // return a Psr-7 response
    }
}



namespace App;

use SomePsr11Container;

use Ellipse\Handlers\ControllerRequestHandler;

use App\Controllers\SomeController;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Register the controller in the container.
$container->set(SomeController::class, function ($container) {

    return new SomeController(new SomeService);

});

// Register some services in the container.
$container->set(SomeOtherService::class, function ($container) {

    return new SomeOtherService;

});

// Those request handlers are using the Psr-11 container, controller class names, methods and attributes.
$handler1 = new ControllerRequestHandler($container, SomeController::class, 'index');
$handler2 = new ControllerRequestHandler($container, SomeController::class, 'show', ['some_id']);
$handler3 = new ControllerRequestHandler($container, SomeController::class, 'store');

// The request handler ->handle() method proxy SomeController index method.
// The contained instance of SomeOtherService is passed to the method.
$response = $handler1->handle($request);

// Here the request handler ->handle() method proxy SomeController show method.
// The contained instance of SomeOtherService is passed to the method.
// The $some_id parameter will receive the request 'some_id' attribute value.
$response = $handler2->handle($request);

// Here the request handler ->handle() method proxy SomeController store method.
// The $request parameter will receive the actual Psr-7 request received by the request handler.
$response = $handler3->handle($request);



// Let controller classes implement some dummy interface specific to the application.

namespace App\Controllers;

class SomeController implements ControllerInterface
{
    // ...
}



namespace App;

use SomePsr11Container;

use Ellipse\Container\ReflectionContainer;
use Ellipse\Handlers\ControllerRequestHandler;

use App\Controllers\ControllerInterface;
use App\Controllers\SomeController;

// Get some Psr-11 container.
$container = new SomePsr11Container;

// Decorate the container with a reflection container.
// Specify the classes implementing ControllerInterface can be auto wired.
$reflection = new ReflectionContainer($container, [
    ControllerInterface::class,
]);

// Create a controller request handler using the reflection container and a controller class name.
$handler = new ControllerRequestHandler($reflection, SomeController::class, 'index');

// An instance of SomeController is built using auto wiring.
$response = $handler->handle($request);