Created
January 20, 2015 14:38
-
-
Save weierophinney/6df6ab0ff9fa36d553dc to your computer and use it in GitHub Desktop.
Conduit examples
This file contains 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