PHP code example of danielspk / tornadohttp

1. Go to this page and download the library: Download danielspk/tornadohttp 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/ */

    

danielspk / tornadohttp example snippets


    $app = new \DMS\TornadoHttp\TornadoHttp();

    $app = new \DMS\TornadoHttp\TornadoHttp(
        [
            ['middleware' => (new MiddlewareClass)],
            ['middleware' => $middlewareOne],
            ['middleware' => 'ServiceMiddlewareTwo'],
            ['middleware' => 'App\MiddlewareThree', 'path' => '/admin'],
            ['middleware' => ['App\MiddlewareFour', [$paramOne, $paramTwo]]],
        ]
    );

    $app = new DMS\TornadoHttp\TornadoHttp(
        [],
        new DefaultResponse()
    );

    $app = new DMS\TornadoHttp\TornadoHttp(
        [],
        new DefaultResponse(),
        new Container()
    );

    $resolver = class () implements ResolverInterface {
        public function solve($middleware) : MiddlewareInterface {
            // ...
        };
    };

    $app = new DMS\TornadoHttp\TornadoHttp(
        [],
        new DefaultResponse(),
        new Container(),
        new $resolver()
    );

    $app = new DMS\TornadoHttp\TornadoHttp(
        [],
        new DefaultResponse(),
        new Container(),
        null,
        'development'
    );

    // mediante el constructor
    $app = new DMS\TornadoHttp\TornadoHttp(
        [
            ['middleware' => (new MiddlewareClass)],
        ]
    );

    // mediante el método add()
    $app->add('ServiceMiddleware', '/', ['GET', 'POST'], ['dev', 'prod'], 0);

    // mediante el método addList()
    $app->addList([
        ['middleware' => (new MiddlewareClass)],
        ['middleware' => 'ServiceMiddleware'],
    ]);

    // mediante una instancia de clase
    $app->add(new MiddlewareClass);

    // mediante un string de referencia a un servicio del contenedor
    $app->add('ServiceMiddleware');

    // mediante un string de referencia a un namespace de clase
    $app->add(App\MiddlewareClass::class);

    // mediante un string de referencia a un namespace de clase y parámetros de constructor
    $app->add([App\MiddlewareClass::class, [$param1, $param2]]);

    // mediante el constructor
    $app = new DMS\TornadoHttp\TornadoHttp(
        [
            [
                'middleware' => (new MiddlewareTimeExecutionClass),
                'path'       => '/admin',
                'env'        => ['develop'],
            ],
            [
                'middleware' => (new MiddlewareLogClass),
                'methods'    => ['POST', 'PUT'],
                'env'        => ['production', 'develop'],
            ],
        ]
    );

    class ExampleMiddleware implements \Psr\Http\Server\MiddlewareInterface
    {
        use \DMS\TornadoHttp\Container\ContainerTrait;

        public function getViewEngine()
        {
            return $this->getContainer()->get('view_engine');
        }

        // ...
    }

    class CustomResolver implements ResolverInterface {
        public function solve(string $middlewareClass) : MiddlewareInterface {
            return new $middlewareClass();
        }
    }

    $app = new DMS\TornadoHttp\TornadoHttp();
    $app->setResolver(new CustomResolver());

    php composer.phar