-
-
Save harikt/2281dc129a1de0f8ca57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Phly\Conduit\Middleware; | |
| use Phly\Conduit\FinalHandler; | |
| use Phly\Conduit\Http\Request as RequestDecorator; | |
| use Phly\Conduit\Http\Response as ResponseDecorator; | |
| use Phly\Http\Response; | |
| use Phly\Http\Server; | |
| use Phly\Http\ServerRequestFactory; | |
| require __DIR__ . '/../vendor/autoload.php'; | |
| $app = new Middleware(); | |
| $request = new RequestDecorator(ServerRequestFactory::fromGlobals( | |
| $_SERVER, | |
| $_GET, | |
| $_POST, | |
| $_COOKIE, | |
| $_FILES | |
| )); | |
| $response = new ResponseDecorator(new Response()); | |
| $server = new Server($app, $request, $response); | |
| $final = new FinalHandler([ 'env' => 'dev' ]); | |
| $app->pipe('/', function ($req, $res, $next) { | |
| if ($req->getUri()->getPath() !== '/') { | |
| return $next(); | |
| } | |
| return $res->end('Hello world!'); | |
| }); | |
| $app->pipe('foo', function ($req, $res, $next) { | |
| return $res->end('FOO!'); | |
| }); | |
| $app->pipe('/redirect', function ($req, $res, $next) { | |
| return $res | |
| ->withStatus(302) | |
| ->withHeader('Location', '/foo'); | |
| }); | |
| $nest = new Middleware(); | |
| $nest->pipe('/foo', function ($req, $res, $next) { | |
| return $res->end('NESTED FOO!'); | |
| }); | |
| $nest->pipe('/redirect', function ($req, $res, $next) { | |
| $fullPath = $req->getOriginalRequest()->getUri()->getPath(); | |
| $path = $req->getUri()->getPath(); | |
| $basePath = substr($fullPath, 0, strrpos($fullPath, $path)); | |
| return $res | |
| ->withStatus(302) | |
| ->withHeader('Location', $basePath . '/foo'); | |
| }); | |
| $app->pipe('/nest', $nest); | |
| $app->pipe(function ($err, $req, $res, $next) { | |
| $res = $res->withStatus(404); | |
| return $res->end('NOT FOUND!'); | |
| }); | |
| $server->listen($final); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment