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 $word is 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 $word finishes with .html, and $dest is undef, then sendHtml() 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 that book() 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::Portal
  • Lemonldap::NG::Handler
  • Plack
  • PSGI
  • Lemonldap::NG::Common::PSGI
  • Lemonldap::NG::Common::PSGI::Request
  • HTML::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. πŸƒβ€β™€οΈπŸ’¨


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