Lemonld...::Router(3)
π Lemonld...::Router(3) π
okay bestie, pull up a chair π«Ά this is the manual page for Lemonldap::NG::Common::PSGI::Router, straight outta the User Contributed Perl Documentation section, and she is the blueprint for REST APIs. no cap.
β¨ NAME β¨
Lemonldap::NG::Common::PSGI::Router β the base library for REST APIs of Lemonldap::NG. π
πΈ SYNOPSIS πΈ
here's the whole vibe, in code:
package My::PSGI;
use base Lemonldap::NG::Common::PSGI::Router;
sub init {
my ($self,$args) = @_;
# Will be called 1 time during startup
# Declare REST routes (could be HTML templates or methods)
$self->addRoute ( 'index.html', undef, ['GET'] )
->addRoute ( books => { ':book' => 'booksMethod' }, ['GET', 'POST'] )
->addRoute ( properties => { '*' => 'propertiesMethod' }, ['GET', 'POST', 'PUT', 'DELETE']);
# Default route (ie: PATH_INFO == '/')
$self->defaultRoute('index.html');
# See Lemonldap::NG::Common::PSGI for other options
# Return a boolean. If false, then error message has to be stored in
# $self->error
return 1;
}
sub booksMethod {
my ( $self, $req, @otherPathInfo ) = @_;
my $book = $req->params('book');
my $method = $req->method;
...
$self->sendJSONresponse(...);
}
sub propertiesMethod {
my ( $self, $property, @otherPathInfo ) = @_;
my $method = $req->method;
...
$self->sendJSONresponse(...);
}
and then this package could literally just be called as a CGI, or using FastCGI, or whatever you're feeling:
#!/usr/bin/env perl
use My::PSGI;
use Plack::Handler::FCGI; # or Plack::Handler::CGI
Plack::Handler::FCGI->new->run( My::PSGI->run() );
it's giving portable. iconic. π
π¦ DESCRIPTION π¦
so basically, this package hands you a base class for the Lemonldap::NG REST API β but you can use it regardless of all that, it doesn't gatekeep. fr fr.
π METHODS π
head over to Lemonldap::NG::Common::PSGI for the logging methods, the content sending, all that good stuff. π
π Initialization methods π
addRoute ( $word, $dest, $methods )
Declares a REST route. the arguments, bestie:
$wordβ the first word of/path/info.$destβ a string, a sub ref, or a hash ref (peep "Route types" right below).$methodsβ an array ref holding the methods this route cares about.
π Route types π
like you saw back in "SYNOPSIS", you can declare routes with a variable component. so $dest can be any of these:
- a word β the name of the method to call.
undefβ then$wordis used as$dest.- a ref to code β an anonymous subroutin to call. (yes, that typo is load-bearing, it's in the original π)
- a hash ref β it's a recursive call to
{ $word => $dest }. - an array ref β in this case each element of the array will be considered as
{ $element => $element }. So each element must be a word that makes a correspondence between a path_info word and a subroutine.
and now some special $word values, pay attention:
':name'β the word in path_info will be stored in GET parameters.'*'β the subroutine will be called with the word of path_info as second argument (after$req).'something.html'β if$wordfinishes with.html, and$destisundef, thensendHtml()will be called with'something.tpl'as template name.
π« Examples π«
to manage http://.../books/127 with book() where 127 is the book number, use:
$self->addRoute( books => { ':bookId' => 'book' }, ['GET'] );
bookId parameter will be stored in $req->params('bookId');
to manage http://.../books/127/pages/5 with page(), use:
$self->addRoute( books => { ':bookId' => { pages => { ':pageId' => 'page' } } }, ['GET'] );
to manage simultaneously the 2 previous examples:
$self->addRoute( books => { ':bookId' => { pages => { ':pageId' => 'page' } } }, ['GET'] )
->addRoute( books => { ':bookId' => { '*' => 'book' } }, ['GET'] );
Note thatbook()will be called for any path_info containing/books/<$bookid>/<$other>except if$other == 'pages'.
to manage /properties/p1, /properties/p2 with p1() and p2(), use:
$self->addRoute( properties => [ 'p1', 'p2' ] );
defaultRoute($path)
This method defined which path_info to use if path_info is / or empty. π
π Accessors π
See Lemonldap::NG::Common::PSGI for inherited accessors (error, languages, logLevel, staticPrefix, templateDir, links, syslog). they're all in the family, no need to reinvent them. π«Ά
π SEE ALSO π
- <http://lemonldap-ng.org/>
Lemonldap::NG::PortalLemonldap::NG::HandlerPlackPSGILemonldap::NG::Common::PSGILemonldap::NG::Common::PSGI::RequestHTML::Template
π§ AUTHORS π§
LemonLDAP::NG team <http://lemonldap-ng.org/team> β absolute legends, we stan. π
π BUG REPORT π
Use OW2 system to report bug or ask for features: <https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
don't be shy, they wanna hear it. π
π DOWNLOAD π
Lemonldap::NG is available at <https://lemonldap-ng.org/download>
go get her. πββοΈπ¨
π COPYRIGHT AND LICENSE π
See COPYING file for details.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
and that's the whole page, bestie β this one was Lemonld...::Router(3) from perl v5.42.3, dated 2026-09-24. go build something slay with it πβ¨
π¦ where this page comes from
- fedora 43: lemonldap-ng-common 2.23.4-1.fc43 Β· GPL-2.0-or-later AND MIT AND GPL-3.0-or-later AND OFL-1.1-RFN
- fedora 44: lemonldap-ng-common 2.23.4-1.fc44 Β· GPL-2.0-or-later AND MIT AND GPL-3.0-or-later AND OFL-1.1-RFN
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms