Skip to content
Joshua Parker edited this page Dec 23, 2020 · 2 revisions

If you return an instance of Response from your closure it will be sent back un-touched. If however you return something else, it will be wrapped in an instance of Response with your return value as the content.

Responsable objects

If you return an object from your closure that implements the Responsable interface, it's toResponse() object will be automatically called for you.

use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Qubus\Routing\Interfaces\Responsable;

class HelloWorldObject implements Responsable
{
    public function toResponse(RequestInterface $request): ResponseInterface
    {
        return new TextResponse('Hello World!');
    }
}

$router->get('hello-world', function () {
    return new HelloWorldObject();
});

404

If no route matches the request, a Response object will be returned with it's status code set to 404;

Accessing current route

The currently matched Route can be retrieved by calling:

$route = $router->currentRoute();

If no route matches or match() has not been called, null will be returned.

You can also access the name of the currently matched Route by calling:

$name = $router->currentRouteName();

If no route matches or match() has not been called or the matched route has no name, null will be returned.

Clone this wiki locally