Hi! I'm starting to learn Dancer2. One question I have is whether it is possible to retrieve the current prefix in some way? 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? Thanks, Ricky The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Mass General Brigham Compliance HelpLine at http://www.massgeneralbrigham.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
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' ) }; }; `/.
Yanick Champoux <yanick@fastmail.com> wrote:
A very simple, straightforward way of doing it:
my $prefix = '/foo'; prefix $prefix; [...]
Thanks! If that's the accepted method, that's simple enough to do. I was hoping there was something that was more a part of Dancer, because then if something weird was going on in the background, my paths would get updated properly. Oh well, Many thanks, Ricky The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Mass General Brigham Compliance HelpLine at http://www.massgeneralbrigham.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
On Thu, 21 Jan 2021 17:12:23 +0000 REMORSE@mgh.harvard.edu wrote:
Hi! I'm starting to learn Dancer2. One question I have is whether it is possible to retrieve the current prefix in some way?
I'm not sure whether this is the correct answer, but can you simply use a variable? E.g. my $PREFIX = '/foo';
prefix '/foo';
Becomes "prefix $PREFIX"
get '/' => sub { forward '/foo'; };
Becomes "forward $PREFIX" And so on. Personally I do: prefix '/:name' And then: my $prefix = route_parameters->get('name') But that assumes you want to match multiple prefixes. Andy
participants (3)
-
Andrew Beverley -
Morse, Richard E.,MGH -
Yanick Champoux