Forward::Guides::Routes::ResourceCustomization - Customizing resourceful routes
The controller parameter and the route name can be changed with the help of the -namespace option:
-namespace
my $r = Forward::Routes->new; $r->add_resources( 'photos', 'users' => -namespace => 'admin', 'prices' ); $m = $r->match(get => 'users'); # $m->[0]->params is {controller => 'Admin::Users', action => 'index'}; my $path = $r->build_path('admin_users_index'); # $path->{path} is 'users' # $path->{method} is 'get'
The -namespace option has no effect on the URL pattern,
# in order to match /admin/users instead of /users, $r->add_route('admin')->add_resources('users' => -namespace => 'admin');
offering a lot more flexibility:
$r->bridge('admin')->to('Authorization#admin') ->add_resources('users' => -namespace => 'admin'); # allows to check whether the current user is really the admin (using # bridges) and break the dispatching cycle if authorization fails
The first part of the URL pattern can be changed with the help of the -as option.
-as
my $r = Forward::Routes->new; $r->add_resources( 'photos', 'users' => -as => 'customers', 'prices' ); $m = $r->match(get => 'customers'); # $m->[0]->params is {controller => 'Users', action => 'index'}; $m = $r->match(get => 'users'); # $m is undef my $path = $r->build_path('users_index'); # $path->{path} is 'customers' # $path->{method} is 'get'
The resource id placeholder can be assigned a custom constraint using the -constraint option:
-constraint
my $r = Forward::Routes->new; $r->add_resources( 'users', 'photos' => -constraints => {id => qr/\d{6}/}, 'tags' ); my $m = $r->match(get => 'photos/123456'); # $m->[0]->params is {controller => 'Photos', action => 'show', id => 123456}; $m = $r->match(get => 'photos/abc'); # $m is undef;
To install Forward::Routes, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Forward::Routes
CPAN shell
perl -MCPAN -e shell install Forward::Routes
For more information on module installation, please visit the detailed CPAN module installation guide.