diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..a26786c --- /dev/null +++ b/Build.PL @@ -0,0 +1,71 @@ +# ========================================================================= +# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. +# DO NOT EDIT DIRECTLY. +# ========================================================================= + +use 5.008_001; + +use strict; +use warnings; +use utf8; + +use Module::Build; +use File::Basename; +use File::Spec; +use CPAN::Meta; +use CPAN::Meta::Prereqs; + +my %args = ( + license => 'perl', + dynamic_config => 0, + + configure_requires => { + 'Module::Build' => 0.38, + }, + + name => 'routes-tiny', + module_name => 'Routes::Tiny', + allow_pureperl => 0, + + script_files => [glob('script/*'), glob('bin/*')], + c_source => [qw()], + PL_files => {}, + + test_files => ((-d '.git' || $ENV{RELEASE_TESTING}) && -d 'xt') ? 't/ xt/' : 't/', + recursive_test_files => 1, + +); +if (-d 'share') { + $args{share_dir} = 'share'; +} + +my $builder = Module::Build->subclass( + class => 'MyBuilder', + code => q{ + sub ACTION_distmeta { + die "Do not run distmeta. Install Minilla and `minil install` instead.\n"; + } + sub ACTION_installdeps { + die "Do not run installdeps. Run `cpanm --installdeps .` instead.\n"; + } + } +)->new(%args); +$builder->create_build_script(); + +my $mbmeta = CPAN::Meta->load_file('MYMETA.json'); +my $meta = CPAN::Meta->load_file('META.json'); +my $prereqs_hash = CPAN::Meta::Prereqs->new( + $meta->prereqs +)->with_merged_prereqs( + CPAN::Meta::Prereqs->new($mbmeta->prereqs) +)->as_string_hash; +my $mymeta = CPAN::Meta->new( + { + %{$meta->as_struct}, + prereqs => $prereqs_hash + } +); +print "Merging cpanfile prereqs to MYMETA.yml\n"; +$mymeta->save('MYMETA.yml', { version => 1.4 }); +print "Merging cpanfile prereqs to MYMETA.json\n"; +$mymeta->save('MYMETA.json', { version => 2 }); diff --git a/META.json b/META.json new file mode 100644 index 0000000..598e436 --- /dev/null +++ b/META.json @@ -0,0 +1,70 @@ +{ + "abstract" : "Routes", + "author" : [ + "Viacheslav Tykhanovskyi, C." + ], + "dynamic_config" : 0, + "generated_by" : "Minilla/v0.7.5, CPAN::Meta::Converter version 2.130880", + "license" : [ + "unknown" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : "2" + }, + "name" : "routes-tiny", + "no_index" : { + "directory" : [ + "t", + "xt", + "inc", + "share", + "eg", + "examples", + "author", + "builder" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "CPAN::Meta" : "0", + "CPAN::Meta::Prereqs" : "0", + "Module::Build" : "0.38" + } + }, + "develop" : { + "requires" : { + "Test::CPAN::Meta" : "0", + "Test::MinimumVersion" : "0.10108", + "Test::Pod" : "1.41", + "Test::Spellunker" : "v0.2.7" + } + }, + "runtime" : { + "requires" : { + "Carp" : "0", + "Scalar::Util" : "0" + } + } + }, + "release_status" : "unstable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/vti/routes-tiny/issues" + }, + "homepage" : "https://github.com/vti/routes-tiny", + "repository" : { + "type" : "git", + "url" : "git://github.com/vti/routes-tiny.git", + "web" : "https://github.com/vti/routes-tiny" + } + }, + "version" : "0.13", + "x_contributors" : [ + "Sergey Zasenko ", + "Dmitry Smal ", + "Dinar Sabitov ", + "vti " + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ee77a7 --- /dev/null +++ b/README.md @@ -0,0 +1,210 @@ +# NAME + +Routes::Tiny - Routes + +# SYNOPSIS + + my $routes = Routes::Tiny->new; + + # Constraints + $routes->add_route('/articles/:id', constraints => {id => qr/\d+/}); + + # Optional placeholders + $routes->add_route('/archive/:year/(:month)?'); + + # Defaults + $routes->add_route('/articles/:id', + defaults => {controller => 'bar', action => 'foo'}); + + # Grouping (matches 'hello-bar') + $routes->add_route('/(:foo)-bar'); + + # Globbing (matches 'photos/foo/bar/baz') + $routes->add_route('/photos/*other'); + + # Path building + $routes->add_route('/:foo/:bar', name => 'default'); + $routes->build_path('default', foo => 'hello', bar => 'world'); + + # Matching + my $match = $routes->match('/hello/world'); + my $captures_hashref = $match->captures; + + # Matching with method + my $match = $routes->match('/hello/world', method => 'GET'); + + # Subroutes + my $subroutes = Routes::Tiny->new; + $subroutes->add_route('/article/:id'); + $routes->mount('/admin/', $subroutes); + +# DESCRIPTION + +[Routes::Tiny](http://search.cpan.org/perldoc?Routes::Tiny) is a lightweight routes implementation. + +[Routes::Tiny](http://search.cpan.org/perldoc?Routes::Tiny) aims to be easy to use in any web framework. + +# FEATURES + +## `Constraints` + + $routes->add_route('/articles/:id', constraints => {id => qr/\d+/}); + + $match = $routes->match('/articles/1'); # Routes::Tiny::Match object + $match = $routes->match('/article/foo'); # undef + +It is possible to specify a constraint that a placeholder must match using a +normal Perl regular expression. + +## `Optional placeholders` + + $routes->add_route('/admin/:service(/:action)?', defaults => {action => 'list'}); + + my $match = $routes->match('/admin/foo'); + # $m->captures is {service => 'foo', action => 'list'} + +It is possible to specify an optional placeholder with a default value. + +## `Grouping` + + $routes->add_route('/(:foo)-bar'); + + $match = $routes->match('/hello-bar'); + # $match->captures is {foo => 'hello'} + +It is possible to create a placeholder that doesn't occupy all the space between +slashes. + +## `Globbing` + + $routes->add_route('/photos/*other'); + $routes->add_route('/books/*section/:title'); + $routes->add_route('/*a/foo/*b'); + + $match = $routes->match('photos/foo/bar/baz'); + # $match->captures is {other => 'foo/bar/baz'} + + $match = $routes->match('books/some/section/last-words-a-memoir'); + # $match->captures is {section => 'some/section', title => 'last-words-a-memoir'} + + $match = $routes->match('zoo/woo/foo/bar/baz'); + # $match->captures is {a => 'zoo/woo', b => 'bar/baz'} + +It is possible to specify a globbing placeholder. + +## `Passing arguments AS IS` + + $routes->add_route('/', arguments => {one => 'two'}); + + $match = $routes->match('/'); + # $match->arguments is {one => 'two'} + +It is possible to pass arguments to the match object AS IS. + +## `Path building` + + $routes->add_route('/articles/:id', name => 'article'); + + $path = $routes->build_path('article', id => 123); + # $path is '/articles/123' + +It is possible to reconstruct a path from route's name and parameters. + +## `Subroutes` + + $subroutes = Routes::Tiny->new; + $subroutes->add_route('/articles/:id', name => 'admin-article'); + $routes->mount('/admin/', $subroutes); + + $match = $routes->match('/admin/articles/3/'); + # $match->captures is {id => 3} + +It is possible to capture params in mount routes + + $subroutes = Routes::Tiny->new; + $subroutes->add_route('/comments/:page/', name => 'comments'); + $routes->mount('/:type/:id/', $subroutes); + + $match = $routes->match('/articles/3/comments/5/'); + # $match->captures is {page => 5} + # $match->parent->captures is {type => 'articles', id => 3} + +Parent routes mounts names of children routes, so it's possible to buil path + + $path = $routes->build_path('admin-article', id => 123); + # $path is '/admin/articles/123' + $path = $routes->build_path('comments', type => 'articles', id => 123, page => 5); + # $path is '/articles/123/comments/5/' + +# WARNINGS + +## `Trailing slash issue` + +Trailing slash is important. + + $routes->add_route('/articles'); + + # is different from + + $routes->add_route('/articles/'); + +If you don't want this behaviour pass `strict_trailing_slash` to the constructor: + + my $routes = Routes::Tiny->new(strict_trailing_slash => 0); + +# METHODS + +## `new` + + my $routes = Routes::Tiny->new; + +## `add_route` + + $routes->add_route('/:service/:action'); + +Add a new route. + +## `mount` + + $routes->mount('/admin/', $subroutes) + +Includes one Routes::Tiny instance into another with given prefix. + +## `match` + + $routes->match('/hello/world'); + +Match against a path. + +## `build_path` + + $pattern->build_path('name', {foo => 'bar'}); + +Build path from a given name and params. + +# DEVELOPMENT + +## Repository + + http://github.com/vti/routes-tiny + +# CREDITS + +Sergey Zasenko (und3f) + +Roman Galeev (jamhed) + +Dmitry Smal (mialinx) + +Dinar (ziontab) + +# AUTHOR + +Viacheslav Tykhanovskyi, `vti@cpan.org`. + +# COPYRIGHT AND LICENSE + +Copyright (C) 2011-2013, Viacheslav Tykhanovskyi + +This program is free software, you can redistribute it and/or modify it under +the terms of the Artistic License version 2.0. diff --git a/minil.toml b/minil.toml new file mode 100644 index 0000000..42c8bb2 --- /dev/null +++ b/minil.toml @@ -0,0 +1 @@ +name = "Routes-Tiny"