NAME

MVC::Neaf::Route - Route (path+method) class for Not Even A Framework

DESCRIPTION

This module contains information about a handler defined using MVC::Neaf: method, path, handling code, connected hooks, default values etc.

It is useless in and off itself.

METHODS

new

Route has the following read-only attributes:

  • parent (required)

  • path (required)

  • method (required)

  • code (required)

  • default

  • cache_ttl

  • path_info_regex

  • param_regex

  • description

  • public

  • caller

  • where

  • tentative

  • override TODO

  • hooks

  • helpers

clone

Create a copy of existing route, possibly overriding some of the fields.

lock()

Prohibit any further modifications to this route.

is_locked

Check that route is locked.

add_form()

add_form( name => $validator )

Create a named form for future query data validation via $request->form("name"). See "form" in MVC::Neaf::Request.

The $validator is one of:

  • An object with validate method accepting one \%hashref argument (the raw form data).

  • A CODEREF accepting the same argument.

Whatever is returned by validator is forwarded into the controller.

Neaf comes with a set of predefined validator classes that return a convenient object that contains collected valid data, errors (if any), and an is_valid flag.

The engine parameter of the functional form has predefined values Neaf (the default), LIVR, and Wildcard (all case-insensitive) pointing towards MVC::Neaf::X::Form, MVC::Neaf::X::Form::LIVR, and MVC::Neaf::X::Form::Wildcard, respectively.

You are encouraged to use LIVR (See Validator::LIVR and LIVR grammar) for anything except super-basic regex checks.

If an arbitrary class name is given instead, new() will be called on that class with \%spec ref as first parameter.

Consider the following script:

use MVC::Neaf;
neaf form => my => { foo => '\d+', bar => '[yn]' };
get '/check' => sub {
    my $req = shift;
    my $in = $req->form("my");
    return $in->is_valid ? { ok => $in->data } : { error => $in->error };
};
neaf->run

And by running this one gets

bash$ curl http://localhost:5000/check?bar=xxx
{"error":{"bar":"BAD_FORMAT"}}
bash$ curl http://localhost:5000/check?bar=y
{"ok":{"bar":"y"}}
bash$ curl http://localhost:5000/check?bar=yy
{"error":{"bar":"BAD_FORMAT"}}
bash$ curl http://localhost:5000/check?foo=137\&bar=n
{"ok":{"bar":"n","foo":"137"}}
bash$ curl http://localhost:5000/check?foo=leet
{"error":{"foo":"BAD_FORMAT"}}

get_form()

$neaf->get_form( "name" )

Fetch form named "name" previously added via add_form to this route or one of its parent routes.

See "form" in MVC::Neaf::Request. See also "add_form".

post_setup

Calculate hooks and path-based defaults.

Locks route, dies if already locked.

INTERNAL LOGIC

The following methods are part of NEAF's core and should not be called unless you want something very special.

dispatch_logic

dispatch_logic( $req, $stem, $suffix )

May die. May spoil request.

Apply controller code to given request object, path stem, and path suffix.

Upon success, return a Neaf response hash (see "THE-RESPONSE" in MVC::Neaf).

LICENSE AND COPYRIGHT

This module is part of MVC::Neaf suite.

Copyright 2016-2023 Konstantin S. Uvarin khedin@cpan.org.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.