Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use HTTP status code constants and JSON/HTML response helpers #114

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ require __DIR__ . '/../vendor/autoload.php';
$app = new FrameworkX\App();

$app->get('/', function () {
return new React\Http\Message\Response(
200,
[],
return React\Http\Message\Response::plaintext(
"Hello wörld!\n"
);
});

$app->get('/users/{name}', function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
[],
return React\Http\Message\Response::plaintext(
"Hello " . $request->getAttribute('name') . "!\n"
);
});
Expand Down
18 changes: 7 additions & 11 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ multiple routes using inline closures like this:

```php
$app->get('/user', function () {
return new React\Http\Message\Response(200, [], "hello everybody!");
return React\Http\Message\Response::plaintext("Hello everybody!\n");
});

$app->get('/user/{id}', function (Psr\Http\Message\ServerRequestInterface $request) {
$id = $request->getAttribute('id');
return new React\Http\Message\Response(200, [], "hello $id");
return React\Http\Message\Response::plaintext("Hello $id!\n");
});
```

Expand Down Expand Up @@ -76,17 +76,17 @@ The `App` also offers a convenient helper method to redirect a matching route to
a new URL like this:

```php
$app->redirect('/promo/reactphp', 'http://reactphp.org/');
$app->redirect('/promo/reactphp', 'https://reactphp.org/');
```

Browsers and search engine crawlers will automatically follow the redirect with
the `302 Found` status code by default. You can optionally pass a custom redirect
status code in the `3xx` range to use. If this is a permanent redirect, you may
want to use the `301 Permanent Redirect` status code to instruct search engine
want to use the `301 Moved Permanently` status code to instruct search engine
crawlers to update their index like this:

```php
$app->redirect('/blog.html', '/blog', 301);
$app->redirect('/blog.html', '/blog', React\Http\Message\Response::STATUS_MOVED_PERMANENTLY);
```

See [response status codes](response.md#status-codes) for more details.
Expand All @@ -98,9 +98,7 @@ examples more concise:

```
$app->get('/', function () {
return new React\Http\Message\Response(
200,
[],
return React\Http\Message\Response::plaintext(
"Hello wörld!\n"
);
});
Expand Down Expand Up @@ -144,9 +142,7 @@ class HelloController
{
public function __invoke()
{
return new Response(
200,
[],
return Response::plaintext(
"Hello wörld!\n"
);
}
Expand Down
14 changes: 7 additions & 7 deletions docs/api/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $app->get(
'/user',
function (Psr\Http\Message\ServerRequestInterface $request, callable $next) {
// optionally return response without passing to next handler
// return new React\Http\Message\Response(403, [], "Forbidden!\n");
// return React\Http\Message\Response::plaintext("Done.\n");

// optionally modify request before passing to next handler
// $request = $request->withAttribute('admin', false);
Expand All @@ -36,7 +36,7 @@ $app->get(
},
function (Psr\Http\Message\ServerRequestInterface $request) {
$role = $request->getAttribute('admin') ? 'admin' : 'user';
return new React\Http\Message\Response(200, [], "Hello $role!\n");
return React\Http\Message\Response::plaintext("Hello $role!\n");
}
);
```
Expand Down Expand Up @@ -65,7 +65,7 @@ class DemoMiddleware
public function __invoke(ServerRequestInterface $request, callable $next)
{
// optionally return response without passing to next handler
// return new React\Http\Message\Response(403, [], "Forbidden!\n");
// return React\Http\Message\Response::plaintext("Done.\n");

// optionally modify request before passing to next handler
// $request = $request->withAttribute('admin', false);
Expand Down Expand Up @@ -156,7 +156,7 @@ class UserController
public function __invoke(ServerRequestInterface $request)
{
$role = $request->getAttribute('admin') ? 'admin' : 'user';
return new Response(200, [], "Hello $role!\n");
return Response::plaintext("Hello $role!\n");
}
}
```
Expand Down Expand Up @@ -235,7 +235,7 @@ class UserController
public function __invoke(ServerRequestInterface $request)
{
$name = 'Alice';
return new Response(200, [], "Hello $name!\n");
return Response::plaintext("Hello $name!\n");
}
}
```
Expand Down Expand Up @@ -416,7 +416,7 @@ a response object synchronously:
$name = yield $promise;
assert(is_string($name));

return new Response(200, [], "Hello $name!\n");
return Response::plaintext("Hello $name!\n");
}

/**
Expand Down Expand Up @@ -455,7 +455,7 @@ a response object synchronously:
{
// async pseudo code to load some data from an external source
return $this->fetchRandomUserName()->then(function (string $name) {
return new Response(200, [], "Hello $name!\n");
return Response::plaintext("Hello $name!\n");
});
}

Expand Down
18 changes: 9 additions & 9 deletions docs/api/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ You can access request attributes like this:
$app->get('/user/{id}', function (Psr\Http\Message\ServerRequestInterface $request) {
$id = $request->getAttribute('id');

return new React\Http\Message\Response(200, [], "Hello $id");
return React\Http\Message\Response::plaintext("Hello $id!\n");
});
```

An HTTP request can be sent like this:

```bash
$ curl http://localhost:8080/user/Alice
Hello Alice
Hello Alice!
```

These custom attributes are most commonly used when using URI placeholders
Expand All @@ -63,15 +63,15 @@ $app->post('/user', function (Psr\Http\Message\ServerRequestInterface $request)
$data = json_decode((string) $request->getBody());
$name = $data->name ?? 'anonymous';

return new React\Http\Message\Response(200, [], "Hello $name");
return React\Http\Message\Response::plaintext("Hello $name!\n");
});
```

An HTTP request can be sent like this:

```bash
$ curl http://localhost:8080/user --data '{"name":"Alice"}'
Hello Alice
Hello Alice!
```

Additionally, you may want to validate the `Content-Type: application/json` request header
Expand All @@ -89,7 +89,7 @@ $app->post('/user', function (Psr\Http\Message\ServerRequestInterface $request)
$data = $request->getParsedBody();
$name = $data['name'] ?? 'Anonymous';

return new React\Http\Message\Response(200, [], "Hello $name");
return React\Http\Message\Response::plaintext("Hello $name!\n");
});
```

Expand All @@ -98,7 +98,7 @@ An HTTP request can be sent like this:

```bash
$ curl http://localhost:8080/user -d name=Alice
Hello Alice
Hello Alice!
```

This method returns a possibly nested array of form fields, very similar to
Expand All @@ -113,7 +113,7 @@ $app->post('/user', function (Psr\Http\Message\ServerRequestInterface $request)
$files = $request->getUploadedFiles();
$name = isset($files['image']) ? $files['image']->getClientFilename() : 'x';

return new React\Http\Message\Response(200, [], "Uploaded $name");
return React\Http\Message\Response::plaintext("Uploaded $name\n");
});
```

Expand Down Expand Up @@ -161,7 +161,7 @@ You can access all HTTP request headers like this:
$app->get('/user', function (Psr\Http\Message\ServerRequestInterface $request) {
$agent = $request->getHeaderLine('User-Agent');

return new React\Http\Message\Response(200, [], "Hello $agent");
return React\Http\Message\Response::plaintext("Hello $agent\n");
});
```

Expand All @@ -184,7 +184,7 @@ $app->get('/user', function (Psr\Http\Message\ServerRequestInterface $request) {
$params = $request->getServerParams();
$ip = $params['REMOTE_ADDR'] ?? 'unknown';

return new React\Http\Message\Response(200, [], "Hello $ip");
return React\Http\Message\Response::plaintext("Hello $ip\n");
});
```

Expand Down
Loading