UR::Se...Router(3pm)

πŸ“¦
version 0.470 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– UR::Service::UrlRouter πŸ’–

okay bestie, so this page lives in the User Contributed Perl Documentation section of the manual (it's a 3pm page, iykyk), and it's all about UR::Service::UrlRouter β€” a PSGI-aware router for incoming requests. yes. a router. for requests. it routes them. slay.


✨ 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 );

(and yes, that second line is exactly what it is. we do not touch it. we do not fix it. we simply observe it with love 🫢)


🌸 DESCRIPTION 🌸

So basically this class is the middleman. the go-between. the group chat admin of your web app πŸ’… It takes requests coming in from a PSGI server and routes each one to the right function that's supposed to handle it. no cap, that's the whole job. match request β†’ hand it to handler.

πŸŽ€ Properties

  • verbose β€” if verbose is true, the object will print the details of the handled requests to STDOUT. loud and proud, honestly. it will narrate. πŸ’€

πŸ¦‹ Methods

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

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

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

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

These four β€” GET, POST, PUT, DELETE β€” are how you register a handler for a given request-method + URL pair. so: this method, this URL, that function. periodt.

The first argument is the URL to match against, and you can hand it over in one of these ways:

  • $string β€” a plain string matches the incoming request when the request's path is eq to that $string. simple. literal. iconic.
  • qr(some regex (with) captures) β€” a regex matches the incoming request when the path matches the regex. and here's the fun part πŸ‘€ if the regex has captures, those captures get passed as additional arguments to the $handler.
  • $coderef β€” a coderef matches the incoming request if $coderef returns true. $coderef is given one argument: the PSGI env hashref.
and $handler? that's a CODE ref. when it gets called, the first argument it receives is the standard PSGI env hashref. always. that's the vibe.

$r->__call__

__call__ is not meant to be called directly. like, don't. πŸ™ˆ

This class overloads the function dereference (call) operator so the object can be used as a callable object β€” as in $obj->(arg, arg). Because that's what overload expects, __call__ returns a code ref, and that code ref handles the PSGI request by finding an appropriate match between the incoming request and a handler you registered earlier. If nothing matches, it returns a 404 error code. bye πŸ‘‹

And if multiple handlers match the incoming request? only the earliest registered handler gets called. first come, first served, no drama. πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘

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


that's the whole page, bestie!! ✨ this one's perl v5.32.1, dated 2022-01-17 β€” so go forth, route those requests, and remember: if nothing matches, it's a 404 and it's not your fault πŸ’–πŸŒˆ

πŸ“¦ where this page comes from