---
name: "UR::Service::UrlRouter"
section: "3pm"
platform: "linux"
package: "libur-perl"
version: "0.470+ds-3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/UR::Service::UrlRouter"
---

# UR::Service::UrlRouter(3pm)

PSGI-aware router for incoming requests

## NAME

UR::Service::UrlRouter - PSGI-aware router for incoming requests

## SYNOPSIS

```
  my $r = UR::Service::UrlRouter->create();
  $r->GET('/index.html', \&handle_index);
  $r->POST(qr(update/(.*?).html, \&handle_update);
  my $s = UR::Service::WebServer->create();
  $s->run( $r );
```

## DESCRIPTION

This class acts as a middleman, routing requests from a PSGI server to the appropriate function to handle the requests.

### Properties

- verbose

  If verbose is true, the object will print details about the handled requests to STDOUT.

### Methods

- $r-\>GET($URLish, $handler)

  - $r-\>POST($URLish, $handler)

  - $r-\>PUT($URLish, $handler)

  - $r-\>DELETE($URLisn, $handler)

  These four methods register a handler for the given request method + URL pair. The first argument specifies the URL to match against,  It can be specified in one of the following ways

  - $string

    A simple string matches the incoming request if the request's path is eq to the `$string`

  - qr(some regex (with) captures)

    A regex matches the incoming request if the path matches the regex.  If the regex contains captures, these are passed as additional arguments to the `$handler`.

  - $coderef

    A coderef matches the incoming request if `$coderef` returns true.  `$coderef` is given one acgument: the PSGI env hashref.

    `$handler` is a CODE ref.  When called, the first argument is the standard PSGI env hashref.

- $r-\>\_\_call\_\_

  \_\_call\_\_ is not intended to be called directly.

  This class overloads the function dereference (call) operator so that the object may be used as a callable object (ie. `$obj`-\>(arg, arg)).  As overload expects, \_\_call\_\_ returns a code ref that handles the PSGI request by finding an appropriate match with the incoming request and a previously registered handler.  If no matching handler is found, it returns a 404 error code.

  If multiple handlers match the incoming request, then only the earliest registered handler will be called.

## SEE ALSO

UR::Service::WebServer, HTTP::Server::PSGI, Plack
