redirectToAction method

Future<void> redirectToAction(
  1. String action, [
  2. Map? params,
  3. int? code
])

Redirects to the given Controller action.

Implementation

Future<void> redirectToAction(String action, [Map? params, int? code]) {
  if (!isOpen) throw closed();
  // UserController@show
  var split = action.split('@');

  if (split.length < 2) {
    throw Exception(
        "Controller redirects must take the form of 'Controller@action'. You gave: $action");
  }

  var controller = app!.controllers[split[0].replaceAll(_straySlashes, '')];

  if (controller == null) {
    throw Exception("Could not find a controller named '${split[0]}'");
  }

  var matched = controller.routeMappings[split[1]];

  if (matched == null) {
    throw Exception(
        "Controller '${split[0]}' does not contain any action named '${split[1]}'");
  }

  final head = controller
      .findExpose(app!.container.reflector)!
      .path
      .toString()
      .replaceAll(_straySlashes, '');
  var tail = '';
  if (params != null) {
    tail = matched
        .makeUri(params.keys.fold<Map<String, dynamic>>({}, (out, k) {
          return out..[k.toString()] = params[k];
        }))
        .replaceAll(_straySlashes, '');
  }
  return redirect('$head/$tail'.replaceAll(_straySlashes, ''), code: code);
}