Reputation: 1077
In order to correctly route the input information within this mojolicious application I am using the 'under' functionality. How come this code doesn't work but the code in the second block does for actually deleting the selected item from the database.
my $r_hostservices = $r->
under('/hosts_services:host_services_id')->
to('hosts_services#hosts_services');
$r_hostservices->
delete(':hosts_services_id')->
to('hosts#hosts_services_deletion')->
name('hosts_services_deletion');
The second (working) block is as follows
$r->delete('/hosts_services/:hosts_services_id')->
to('hosts#hosts_services_deletion')->
name('hosts_services_deletion');
Upvotes: 3
Views: 307
Reputation: 687
package a;
use Mojo::Base 'Mojolicious';
# This method will run once at server start
sub startup {
my $self = shift;
# Load configuration from hash returned by config file
my $config = $self->plugin('Config');
# Configure the application
$self->secrets($config->{secrets});
# Router
my $r = $self->routes;
my $ur = $r->under('/foo/:bar');
$ur->delete()->to('example#two');
$ur->any()->to('example#one');
}
1;
You can always list all defined routes this way:
script/a routes
This feature very useful for debugging in such situations. You can read more about Mojolicious CLI here Mojolicious::Commands.
Here comes example output with routes mentioned above:
/foo/:bar * foobar
+/ DELETE
+/
*
So, we can see, that Mojolicious now will serve url /foo/:bar/
for DELETE
and any other request method, but trailing slash is always optional, and /foo/:bar
will work fine.
In this case: under('/foo/:bar')->to( example#zero )
, example#zero
is intermediate destination, so all routes defined under this one, will visit example#zero
and only after it will continue execution of own destination. So, under( ... )->to( ... )
can be used for auth and another similar tasks. In my example under()
used without to()
.
P.S. Mojolicious comes with wonderful documentation and I encourage everyone to actively use it
Upvotes: 2