Example #1
0
|--------------------------------------------------------------------------
| Advanced Example
|--------------------------------------------------------------------------
|
| This example uses the request to route the request to the correct page,
| this behaviour is common in PHP router packages.
|
*/
$request = \Sephedo\Http\ServerRequest::buildServerRequest();
/**
 * Normally $request->getRequestTarget would be used but for an easy
 * example and usage we will use a query parameter of "p" to indicate which
 * page we should load for example ?p=about would be the about page.
 */
$requestTarget = isset($request->getQueryParams()['p']) ? $request->getQueryParams()['p'] : 'home';
$response = \Sephedo\Http\Response::buildResponse();
$response->getBody()->write("<h1>Advanced Example</h1>\n");
/**
 * The routing logic is typically more complex then this but switching the
 * requestTarget will work for this example.
 */
switch (strtolower($requestTarget)) {
    case 'home':
        // Generate a URI to the about page.
        $uri = $request->getUri()->withQuery('?p=about');
        // Generate some HTML as a response.
        $response->getBody()->write("<h3>Home Page.</h3>\n");
        $response->getBody()->write("<p>Welcome to my awesome home page.</p>\n");
        $response->getBody()->write("<p><a href=\"{$uri}\">Read more about me</a>.\n");
        break;
    case 'about':