++ed by:

2 PAUSE users
2 non-PAUSE users.



Forward::Guides::Routes::ResourceCustomization - Customizing resourceful routes


The -namespace option works like the namespace command. It might be needed to determine the full controller class name ($match->class). It also effects the names automatically generated for your routes, so make sure to use app_namespace instead of namespace for your application base namespace to keep route names as short as possible (use namespace for sub namespaces only).

    my $r = Forward::Routes->new;
    $r->add_resources('users' => -namespace => 'Admin');

    # which is equivalent to
    my $r = Forward::Routes->new->namespace('Admin');

    $m = $r->match(get => 'users');
    # controller param is still 'Users'...
    # $m->[0]->params is {controller => 'Users', action => 'index'};
    # ... but:
    # $m->[0]->class is "Admin::Users"
    # $m->[0]->action is "index"
    # $m->[0]->name is "admin_users_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:

      ->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

Changes of a resources namespace also effects nested resources (inheritance)!


The first part of the URL pattern can be changed with the help of the -as option.

    my $r = Forward::Routes->new;
        'users' => -as => 'customers',

    $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:

    my $r = Forward::Routes->new;
        'photos' => -constraints => {id => qr/\d{6}/},
    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;


The format constraint can be changed with the help of the -format option:

    my $r = Forward::Routes->new;
        'photos' => -format => 'html',
    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
    $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.


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;
        'users' => -only => ['create', 'show'],
    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};