hyperf/docs/en/controller.md

42 lines
2.6 KiB
Markdown
Raw Normal View History

2019-12-12 16:24:04 +08:00
# Controller
2019-03-30 12:48:59 +08:00
To process HTTP requests by using Controller, you need to bind routing and controller methods in `Config` or `Annotation` way. Check the chapter [Router](en/route.md) for more details.
For the `Request` and `Response`, Hyperf provided `Hyperf\HttpServer\Contract\RequestInterface` and `Hyperf\HttpServer\Contract\ResponseInterface` for you to get parameters and return values. Check the chapters [Request](en/request.md) 与 [Response](en/response.md) for more details.
2019-03-30 12:48:59 +08:00
2019-12-12 16:24:04 +08:00
## Create a Controller
2019-03-30 12:48:59 +08:00
```php
<?php
declare(strict_types=1);
2019-03-30 20:55:38 +08:00
namespace App\Controller;
2019-03-30 12:48:59 +08:00
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
class IndexController
{
// Related objects will be automatically injected by the dependency injection container if you obtain such objects by defining RequestInterface and ResponseInterface on the parameters.
2019-03-30 12:48:59 +08:00
public function index(RequestInterface $request, ResponseInterface $response)
{
2019-03-30 20:55:38 +08:00
$target = $request->input('target', 'World');
return 'Hello ' . $target;
2019-03-30 12:48:59 +08:00
}
}
```
> Assume this `Controller` has been defined as `/` route through `Config`. (Of course, you can also define it through `Annotation`)
2019-03-30 18:52:20 +08:00
Call this address through `cURL`, and you can see the returned content.
2019-03-30 12:48:59 +08:00
```bash
2019-03-30 18:52:20 +08:00
$ curl http://127.0.0.1:9501/\?target\=Hyperf
Hello Hyperf.
2019-03-30 12:48:59 +08:00
```
## Avoid data confusion between coroutines
2019-05-07 22:16:46 +08:00
In the traditional PHP-FPM framework, an `AbstractController` (or an abstract parent class in other names) would be provided. Then, other defined `Controller` will perform some requests or responses based on the `AbstractController`. However, in Hyperf, **DON'T DO LIKE THAT**. Since most objects, including `Controller`, exist as `Singleton`, which is also for better reuse of objects, and request data are stored at `Context` in coroutine, so **DO NOT** store any request data as a class attribute (non-static properties included).
2019-05-07 22:16:46 +08:00
Of course, it's not impossible if you really want to store request data as class attributes. We have noticed that `Request` and `Response` objects are obtained through injecting `Hyperf\HttpServer\Contract\RequestInterface` and `Hyperf\HttpServer\Contract\ResponseInterface` when we trying to get `Request` and `Response`. Isn't the corresponding object also a singleton? How is the coroutine safe here? Take the `RequestInterface` as an example, when the corresponding `Hyperf\HttpServer\Request` object gets `PSR-7 request object` from its internal, it is obtained from the `Context`. So the actual class used is only a proxy class, and the actual call is obtained from the `Context`.