opsbears / piccolo-web-processor-controller
Requires
- php: ^7.0
- opsbears/piccolo-web: ^1.0
- opsbears/piccolo-web-http: 1.0.0
Requires (Dev)
Suggests
Provides
This package is not auto-updated.
Last update: 2020-10-16 21:32:36 UTC
README
The controller processor is module that implements Piccolo\Web\Processor\RequestProcessor
through the
Piccolo\Web\Processor\Controller\ControllerRequestProcessor
class. It's primary purpose is to call the routing
class defined in Piccolo\Web\Routing\Router
, and then call the class/method that was returned from the routing
process. The response from the method is processed as outlined below.
Writing a controller
To write a controller, you simply have to create a class with any function, and return one of the following:
- PSR-7 response: if a PSR-7 response is returned, the contents of
HTTPRequestResponseContainer
are ignored and the response is returned to the client unmodified. - string: the string response is immediately returned to the output. Additional headers can be set by
requesting the injection of
HTTPRequestResponseContainer
to the controller. - array: if an array is returned, the view processor (an implementation of
Piccolo\Web\Processor\Controller\ControllerViewProcessor
) is called to process the data into a string. The string is then returned to the client, just as described above.
To get any of the parameters in your method, you can simply add them as a type hint, and the dependency injection container will take care of the rest. This will work automatically:
class MyController {
class myAction(ServerRequestInterface $request) {
//...
}
}
Tip: In order for this to work, you will obviously have to add your controller to your routing configuration.
Tip: always explicitly declare your dependencies, don't try to get a hold of the Dependency Injection Container to get services. That would turn it into a service locator, which is an anti-pattern.