-
-
Notifications
You must be signed in to change notification settings - Fork 2
Advanced Guide
If you want, you can customize the behaviour of WaterPipe using the WaterPipeConfig class. Available options are:
-
queryStringEnabled: This option is abooleandefining if the use of query strings are enabled in your pipes. The default value istrue; -
defaultCharset: This option is astringdefining the default charset to use when processing responses. The default value is"utf-8"; -
useStdrr: This option is abooleandefining if WaterPipe have to use theSTDERRoutput channel to print errors and uncaught exceptions. The default value istrue.
To configure WaterPipe, you just have to retrieve the singleton and define values, before creating pipes.
<?php
use \ElementaryFramework\WaterPipe\WaterPipeConfig;
// Edit configuration
$config = WaterPipeConfig::get();
$config->setUseStderr(false);
$config->setDefaultCharset("ISO-8859-1");
// You can now create your pipes...There are several ways to create routes with WaterPipe. Each of them has been designed to match common Web Design Patterns and architectures.
This is the way you have seen in the Getting Started guide. Here you create your URI callback in an anonymous function, wrapping as parameters the Request and the Response of the current context.
$pipe->request('/hello/to/world', function (Request $req, Response $res) {
$res->sendText("Hello, world !");
});By this way, you have the hability to leave URIs and their callbacks at the same place. It can help you for maintenenance, but can make the file hard to edit if many routes are created into it...
Using RouteActions
RouteAction help you wrap your URI callback in a specific class. This class must extend the abstract RouteAction class and implement the execute() method. Using RouteAction, you have to access the Request and the Response of the current context through RouteAction::_request and RouteAction::_response properties.
class RouteActionExample extends \ElementaryFramework\WaterPipe\Routing\RouteAction
{
public function execute()
{
$this->_response->sendText("Hello, world !");
}
}
$pipe->request('/hello/to/world', RouteActionExample::class);
// -- OR --
$pipe->request('/hello/to/world', new RouteActionExample);The main advantage of RouteAction is the code reuse when many URI have the same callback (or the callback change only according to URI parameters).
Besides anonymous functions, WaterPipe support any kind of callable forms supported by PHP.
// Using static method with callable array form
$pipe->request('/hello/to/world', [HelloWorldController::class, 'helloToWorldStatic']);
// Using instance method with callable array form
$pipe->request('/hello/to/world', [new HelloWorldController, 'helloToWorld']);
// Using function names
function helloWorld(Request $req, Response $res) { $res->sendText("Hello, world !"); }
$pipe->request('/hello/to/world', 'helloworld');
// Using closures
$pipe->request('/hello/to/world', \Closure::fromCallable(/* Any previous callable form as parameter */));Note it is important that the called function in the callable form must have as parameters the Request and the Response.