[dancer-users] Retrieving the current prefix?
Yanick Champoux
yanick at fastmail.com
Thu Jan 21 17:19:48 EST 2021
On 2021-01-21 12:12 p.m., Morse, Richard E.,MGH wrote:
> In particular, I am showing a listing of things, and I would like to add an edit link. I have the following setup:
>
> prefix '/foo';
>
> get '/' => sub { forward '/foo'; };
> get '' => sub {
> my @rows = database->quick_select(...);
> foreach my $r (@rows) {
> $r->{'edituri'} = uri_for( '/foo/' . $r->{'id'});
> }
> template 'foo.tt', { 'listing' => \@rows, 'adduri' => uri_for( '/foo/add' ) };
> };
>
> I feel like I should be able to get rid of the '/foo' bit in the `forward` and `uri_for` calls — if this changes, I now need to change it in multiple places, and could introduce a typo. I tried doing `uri_for( prefix . '/' . $r->{'id'})`, but that didn't work.
>
> If there isn't some obvious way to do this, how to other people manage these kinds of things?
A very simple, straightforward way of doing it:
my $prefix = '/foo';
prefix $prefix;
get '/' => sub { forward '/foo'; };
get '' => sub {
my @rows = database->quick_select(...);
foreach my $r (@rows) {
$r->{'edituri'} = uri_for( join '/', $prefix', $r->{'id'});
}
template 'foo.tt', { 'listing' => \@rows, 'adduri' => uri_for( join '/', $prefix, 'add' ) };
};
`/.
More information about the dancer-users
mailing list