Ado

 view release on metacpan or  search on metacpan

lib/Ado/Plugin/Routes.pm  view on Meta::CPAN

package Ado::Plugin::Routes;
use Mojo::Base 'Ado::Plugin';

sub register {
    my ($self, $app, $conf) = shift->initialise(@_);

    #Add some conditions: Someday
#    $app->routes->add_condition(
#        require_formats => sub {
#            my ($route, $c, $captures, $formats) = @_;
#            $c->debug('$route, $c, $captures, $formats:'
#                  . $c->dumper( $route, ref $c, $captures, $formats));
#             #Carp::cluck(caller);
#            return ($c->require_formats($formats) ? 1 : undef);
#        }
#    );

    # Rewrite urls in case we are deployed under Apache and using mod_cgi or mod_fcgid.
    # This way we have the same urls as if deployed standalone or with hypnotoad.
    # See templates/partials/apache2htaccess.ep
    $app->hook(
        before_dispatch => sub {
            state $cgi = $_[0]->req->env->{GATEWAY_INTERFACE} // '' =~ m/^CGI/;
            $_[0]->req->url->base->path($conf->{base_url_path}) if $cgi;
        }
    ) if $conf->{base_url_path};

    return $self;
}


1;


=pod

=encoding utf8

=head1 NAME

Ado::Plugin::Routes - Keep routes separately.


=head1 SYNOPSIS

  #Open $MOJO_HOME/etc/plugins/routes.conf and describe your routes
  routes     => [
        {route => '/ado-users', via => ['GET'],
          to => 'ado-users#list',},
        {route => '/ado-users', via => ['POST'],
          to => 'ado-users#add',},
        ...
        ],
  base_url_path =>'/'

=head1 DESCRIPTION

Ado::Plugin::Routes allows you to define your routes in a separate file
C<$MOJO_HOME/etc/plugins/routes.conf>. In the configuration file you can also
use the B<C<app>> keyword and add complex routes as you would do directly in
the code.

=head1 OPTIONS

=head2 base_url_path

Base URL which will be used to construct URLs when deployed as FCGI or CGI.
Default: C<undef>.

=head2 routes

And ARRAY reference describing the routes.

=head1 METHODS


L<Ado::Plugin::Routes> inherits all methods from
L<Ado::Plugin> and implements the following new ones.



( run in 0.508 second using v1.01-cache-2.11-cpan-5735350b133 )