X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole.pm;h=14260bd4320eb2ecfbf57591cbc5493b0fd5c50a;hb=55f97a4ef2080f9fa90d5a85b703f23df76aa815;hp=bec9675fda4f1fb643f1c43a0f123bfffd93ce27;hpb=01eab49507a01b9b6135ca1fdd085094373cd78f;p=maypole.git diff --git a/lib/Maypole.pm b/lib/Maypole.pm index bec9675..14260bd 100644 --- a/lib/Maypole.pm +++ b/lib/Maypole.pm @@ -6,6 +6,7 @@ use warnings; use Maypole::Config; use Maypole::Constants; use Maypole::Headers; +use URI(); our $VERSION = '2.11'; @@ -355,6 +356,95 @@ sub parse_path $self->args(\@pi); } +=head3 make_path( %args or \%args or @args ) + +This is the counterpart to C. It generates a path to use +in links, form actions etc. To implement your own path scheme, just override +this method and C. + + %args = ( table => $table, + action => $action, + additional => $additional, # optional - generally an object ID + ); + + \%args = as above, but a ref + + @args = ( $table, $action, $additional ); # $additional is optional + +C can be used as an alternative key to C. + +C<$additional> can be a string, an arrayref, or a hashref. An arrayref is +expanded into extra path elements, whereas a hashref is translated into a query +string. + +=cut + +sub make_path +{ + my $r = shift; + + my %args; + + if (@_ == 1 and ref $_[0] and ref $_[0] eq 'HASH') + { + %args = %{$_[0]}; + } + elsif ( @_ > 1 and @_ < 4 ) + { + $args{table} = shift; + $args{action} = shift; + $args{additional} = shift; + } + else + { + %args = @_; + } + + do { die "no $_" unless $args{$_} } for qw( table action ); + + my $additional = $args{additional} || $args{id}; + + my @add = (); + + if ($additional) + { + # if $additional is a href, make_uri() will transform it into a query + @add = (ref $additional eq 'ARRAY') ? @$additional : ($additional); + } + + my $uri = $r->make_uri($args{table}, $args{action}, @add); + + return $uri->as_string; +} + +=head3 make_uri( @segments ) + +Make a L object given table, action etc. Automatically adds +the C. + +If the final element in C<@segments> is a hash ref, C will render it +as a query string. + +=cut + +sub make_uri +{ + my ($r, @segments) = @_; + + my $query = (ref $segments[-1] eq 'HASH') ? pop(@segments) : undef; + + my $base = $r->config->uri_base; + $base =~ s|/$||; + + my $uri = URI->new($base); + $uri->path_segments($uri->path_segments, grep {length} @segments); + + my $abs_uri = $uri->abs('/'); + $abs_uri->query_form($query) if $query; + return $abs_uri; +} + + # like CGI::param(), but read only sub param { @@ -685,6 +775,8 @@ The named parameters are protocol, domain, path, status and url Only 1 named parameter is required but other than url, they can be combined as required and current values (from the request) will be used in place of any missing arguments. The url argument must be a full url including protocol and can only be combined with status. +=head3 redirect_internal_request + =head3 handler This method sets up the class if it's not done yet, sets some