diff --git a/vagrant/index.php b/vagrant/index.php index a6bbc64..e9c02d9 100644 --- a/vagrant/index.php +++ b/vagrant/index.php @@ -1,11 +1,54 @@ addPsr4("", __DIR__ . "/../vagrant/src"); $loader->addPsr4("", __DIR__ . "/../autoload"); -$router = new Router(); -$router->add("/", "\\WellRESTedDev\\RootHandler"); -$router->respond(); + +// Build some middleware. We'll register these with a server below. +// We're using callables to fit this all in one example, but these +// could also be classes implementing WellRESTed\MiddlewareInterface. + +// Set the status code and provide the greeting as the response body. +$hello = function ($request, $response, $next) { + + // Check for a "name" attribute which may have been provided as a + // path variable. Use "world" as a default. + $name = $request->getAttribute("name", "world"); + + // Set the response body to the greeting and the status code to 200 OK. + $response = $response->withStatus(200) + ->withHeader("Content-type", "text/plain") + ->withBody(new Stream("Hello, $name!")); + + // Propagate to the next middleware, if any, and return the response. + return $next($request, $response); + +}; + +// Add a header to the response. +$headerAdder = function ($request, $response, $next) { + // Add the header. + $response = $response->withHeader("X-example", "hello world"); + // Propagate to the next middleware, if any, and return the response. + return $next($request, $response); +}; + +// Create a server +$server = new Server(); + +// Start each request-response cycle by dispatching the header adder. +$server->add($headerAdder); + +// The header adder will propagate to this router, which will dispatch the +// $hello middleware, possibly with a {name} variable. +$server->add($server->createRouter() + ->register("GET", "/hello", $hello) + ->register("GET", "/hello/{name}", $hello) +); + +// Read the request from the client, dispatch middleware, and output. +$server->respond(); diff --git a/vagrant/src/WellRESTedDev/RootHandler.php b/vagrant/src/WellRESTedDev/RootHandler.php deleted file mode 100644 index 1336d66..0000000 --- a/vagrant/src/WellRESTedDev/RootHandler.php +++ /dev/null @@ -1,50 +0,0 @@ - - - - - WellRESTed - - -

Welcome to the WellRESTed Test Site

- -

Run vagrant ssh, then:

-
-
To run unit tests
-
vendor/bin/phpunit
-
To generate documentation
-
make html -C docs
-
-

Use this site as a sandbox. Modify the router /htdocs/index.php however you like.

-

Any classes you create inside /autoload will be autoloaded with a PSR-4 autoloader.

- - -HTML; - $response = new Response(200); - $response->setBody($view); - return $response; - } -}