PHP code example of alvinios / miel

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

    

alvinios / miel example snippets


use Alvinios\Miel\App;
use Alvinios\Miel\Http\Emit;
use Alvinios\Miel\Response\Text;
use Alvinios\Miel\Fork\Regex;
use GuzzleHttp\Psr7\{HttpFactory, ServerRequest};

(new Emit())(
    (new App(
        new Regex('/', new Text('Hello world!'))
    ))->response(ServerRequest::fromGlobals(), new HttpFactory(), new HttpFactory())
);

use Alvinios\Miel\App;
use Alvinios\Miel\Http\Emit;
use Alvinios\Miel\Endpoint\Base;
use Alvinios\Miel\Request\WithRegex;
use Alvinios\Miel\Response\{Json, Response, Text, Twig}; 
use Alvinios\Miel\Fork\{Regex, Methods};
use GuzzleHttp\Psr7\{HttpFactory, ServerRequest};
use Psr\Http\Message\ServerRequestInterface;

(new Emit())(
  (new App(
        new Regex('/', new Text('Hello world!')),
        new Regex(
            '/users/(?P<id>[\d]+)',
            new Methods(
                ['post'],
                new class() extends Base {
                    public function act(ServerRequestInterface $request): Response {
                        return new Json(
                            json_decode($request->getBody()->getContents())
                        );
                    }
                }         
            ),
            new Methods(
                ['get'],
                new class() extends Base {
                    public function act(ServerRequestInterface|WithRegex $request): Response {
                        return new Text(sprintf(
                            '<html><body>User %s</body></html>',
                            $request->regex()->group('id')
                        ));
                    }
                }      
            )
        )
  ))->response(ServerRequest::fromGlobals(), new HttpFactory(), new HttpFactory())
);

use Alvinios\Miel\App;
use Alvinios\Miel\Response\{Text, Twig}; 
use Alvinios\Miel\Fork\{Append, Regex};

 new App(
     new Append(
         call_user_func(function() : \Iterator {
             yield new Regex('^(/|/home)$', new Twig($this->twig, 'index.html.twig', []));
         }),
         call_user_func(function() : \Iterator {
             yield new Regex('/foo', new Text('Foo'));
             yield new Regex('/bar', new Text('Bar'));
         })
     )
)

use Alvinios\Miel\Fork\{Regex, Shields};
use Psr\Http\Server\MiddlewareInterface;

new Shields(
    new Regex('/foo', new Text('Behind middleware')),
    new class() implements MiddlewareInterface {
       ...
    },
    new class() implements MiddlewareInterface {
       ...
    }
)

use Alvinios\Miel\Fork\Shield;
use Psr\Http\Server\MiddlewareInterface;

new Shield(
    new class() implements MiddlewareInterface {
        ...
    },
    new Regex('/foo', new Text('Behind middleware'))
)

php -S localhost:8000