forwardTo method

FutureOr<Result> forwardTo(
  1. Request request
)

Forwards request through the RelicRouter that routed this request.

This re-routes request through the same router that handled the current request, enabling URL rewriting and internal redirects.

Typically used with Request.copyWith to change the URL:

router.get('/old-path', (req) {
  final newReq = req.copyWith(
    url: req.url.replace(path: '/new-path'),
  );
  return req.forwardTo(newReq);
});

Throws a StateError if this request was not routed through a RelicRouter.

Implementation

FutureOr<Result> forwardTo(final Request request) {
  final r = router;
  if (r == null) {
    throw StateError(
      'Cannot forward: no RelicRouter found. '
      'Ensure this request was routed through a RelicRouter.',
    );
  }
  return r.asHandler(request);
}