Amon2-Web-Dispatcher-RouterSimple-Extended

 view release on metacpan or  search on metacpan

lib/Amon2/Web/Dispatcher/RouterSimple/Extended.pm  view on Meta::CPAN

    my ($caller, $method) = @_;

    sub {
        my $class = caller(0);

        if ($submap) {
            my ($path, $action) = @_;
            $submap->connect($path, { action => $action }, { metod => $method });
        } else {
            $_[2] = { method => $method };
            goto \&_connect;
        }
    }
}

sub _connect {
    my $caller = caller(0);
    if ($submap) {
        if (@_ >= 2 && !ref $_[1]) {
            my ($path, $action, $opt) = @_;
            $submap->connect($path, { action => $action }, $opt || {});
        } else {
            $submap->connect(@_);
        }
    } else {
        my $router = $caller->router;
        if (@_ >= 2 && !ref $_[1]) {
            my ($path, $dest_str, $opt) = @_;
            my ($controller, $action) = split('#', $dest_str);
            my $dest = { controller => $controller };
            $dest->{action} = $action if defined $action;
            $router->connect($path, $dest, $opt || {});
        } else {
            $router->connect(@_);
        }
    }
}

sub _submapper {
    my $caller = caller(0);
    my $router = caller(0)->router();
    if ($_[2] && ref($_[2]) eq 'CODE') {
        my ($path, $controller, $callback) = @_;
        $submap = $router->submapper($path, { controller => $controller });
        $callback->();
        undef $submap;
    }
    else {
        $router->submapper(@_);
    }
}

sub _dispatch {
    my ($class, $c) = @_;
    my $req = $c->request;
    if (my $p = $class->match($req->env)) {
        my $action = $p->{action};
        $c->{args} = $p;
        "@{[ ref Amon2->context ]}::C::$p->{controller}"->$action($c, $p);
    } else {
        $c->res_404();
    }
}

1;
__END__

=encoding utf-8

=head1 NAME

Amon2::Web::Dispatcher::RouterSimple::Extended - extending Amon2::Web::Dispatcher::RouterSimple

=head1 SYNOPSIS


    package MyApp::Web::Dispatcher;
    use strict;
    use warnings;
    use utf8;
    use Amon2::Web::Dispatcher::RouterSimple::Extended;
    connect '/' => 'Root#index';
    # API
    submapper '/api/' => API => sub {
        get  'foo' => 'foo';
        post 'bar' => 'bar';
    };
    # user
    submapper '/user/' => User => sub {
        get     '',           'index';
        connect '{uid}',      'show';
        post    '{uid}/hoge', 'hoge';
        connect 'new',        'create';
    };
    1;

=head1 DESCRIPTION

This is an extension of Amon2::Web::Dispatcher::RouterSimple. 100% compatible, and it provides useful functions.


=head1 METHODS

=over 4

=item get $path, "${controller}#${action}"

this is equivalent to:

    connect $path, { controller => $controller, action => $action }, { method => 'GET' };


=item post $path, "${controller}#${action}"

this is equivalent to:

    connect $path, { controller => $controller, action => $action }, { method => 'POST' };


=item put $path, "${controller}#${action}"



( run in 1.517 second using v1.01-cache-2.11-cpan-39bf76dae61 )