Skip to content

Instantly share code, notes, and snippets.

@masaki
Created Apr 6, 2009
Embed
What would you like to do?
# Merb-like
use HTTP::Router::Declare;
my $router = router {
# path and params
match '/' => to { controller => 'Root', action => 'index' };
# path, conditions, and params
match '/home', { method => 'GET' } => to { controller => 'Home', action => 'show' };
match '/date/{year}', { year => qr/^\d{4}$/ } => to { controller => 'Date', action => 'by_year' };
# path, params, and nesting
match '/account' => to { controller => 'Account' } => then {
match '/login' => to { action => 'login' };
match '/logout' => to { action => 'logout' };
};
# path nesting
match '/account' => then {
match '/signup' => to { controller => 'Users', action => 'register' };
match '/logout' => to { controller => 'Account', action => 'logout' };
};
# conditions nesting
match { method => 'GET' } => then {
match '/search' => to { controller => 'Items', action => 'search' };
match '/tags' => to { controller => 'Tags', action => 'index' };
};
# params nesting
with { controller => 'Account' } => then {
match '/login' => to { action => 'login' };
match '/logout' => to { action => 'logout' };
};
# match only
match '/{controller}/{action}/{id}.{format}';
match '/{controller}/{action}/{id}';
# create resources
resources 'Users';
resources 'Users', { only => ['index', 'show'] };
resources 'Users', { except => ['delete', 'destroy'] };
# user defined resources
# GET /users/feeds
# GET /users/{user_id}/settings
resources 'Users', {
collection => { feeds => 'GET' },
member => { settings => 'GET' },
};
# nested resources
# /users/{user_id}/articles, /users/{user_id}/articles/{article_id}, ...
resources 'Users', then {
resources 'Articles';
};
# create singleton resource
resource 'Account';
resource 'Account', { only => ['show', 'update'] };
resource 'Account', { except => ['delete', 'destroy'] };
# user defined singleton resource
# GET /account/settings
resource 'Account', { member => { settings => 'GET' } };
# nested singleton resource to resource(s)
# 1. resource 'Admin' to /account/admin
# 2/ resources 'Articles' to /account/articles, /account/articles/{article_id}, ...
resource 'Account', then {
resource 'Admin';
resources 'Articles';
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment