Name
Forward::Guides::Routes::ResourceCustomization - Customizing resourceful routes
namespace
The controller parameter and the route name can be changed with the help of the -namespace
option:
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');
This offers 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
as
The first part of the URL pattern can be changed with the help of the -as
option.
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'
constraints
The resource id placeholder can be assigned a custom constraint using the -constraint
option:
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;
format
The format constraint can be changed with the help of the -format
option:
my $r = Forward::Routes->new;
$r->add_resources(
'users',
'photos' => -format => 'html',
'tags'
);
my $m = $r->match(get => 'photos/123456.html');
# $m->[0]->params is {format => 'html', controller => 'Photos', action => 'show',
# id => 123456};
$m = $r->match(get => 'photos/123456');
# $m is undef;
Resources inherit the file extension of their parent. As a result, the format constraint can also be changed through format inheritance:
# in order to match get => /cities/paris/edit.html
# Routes root object
my $root = Forward::Routes->new;
# Create a parent route with format "html"
$route_with_html_extension = $root->add_route->format('html');
# add a resource on top of parent
$route_with_html_extension->add_resources('cities');
$m = $root->match(get => '/cities/paris/edit.html');
# $m->[0]->params is
# {controller => 'Cities', action => 'cities_update_form',
# format => 'html'};
The inherited format behaviour can be overwritten with the -format
option for individual resources.
only
Instead of creating all default resourceful routes for a resource (i.e. index, create, show, update, delete, create_form, update_form, delete_form), only selected routes can be generated using the -only
option.
# only generate the "create" and "show" routes for the "users" resource
my $root = Forward::Routes->new;
$root->add_resources(
'photos',
'users' => -only => ['create', 'show'],
'prices'
);
my $m = $root->match(get => 'users');
# $m is undef, as "index" not included via "only"
$m = $root->match(post => 'users');
# create:
# $m->[0]->params is {controller => 'Users', action => 'create'};
$m = $root->match(get => 'users/1');
# show
# $m->[0]->params is {controller => 'Users', action => 'show', id => 1};