diff --git a/Changes b/Changes index 4284628ac..11940db1f 100644 --- a/Changes +++ b/Changes @@ -8,6 +8,7 @@ (Sawyer X) [ DOCUMENTATION ] + * Migration document! (Snigdha Dagar) * GH #667: Fix typo in cookbook pod. (Lindsey Beesley) * GH #649, #670: Document core logger. (simbabque) * GH #689: Git guide markdown fixes. (Paul Cochrane) diff --git a/lib/Dancer2/Manual/Migration.pod b/lib/Dancer2/Manual/Migration.pod new file mode 100644 index 000000000..3243ce2b6 --- /dev/null +++ b/lib/Dancer2/Manual/Migration.pod @@ -0,0 +1,193 @@ +package Dancer2::Manual::Migration; + +use strict; +use warnings; + +1; + +__END__ + +=pod + +=head2 Migration from Dancer1 to Dancer2 + +This document covers some changes that users will need to be aware of +while upgrading from L (version 1) to L. + +=head3 Apps + +1. In L, each module is a B with its own +namespace and variables. You can set the application name in each of your +L application modules. Different modules can be tied into the same +app by setting the application name to the same value. + +For example, to set the appname directive explicitly: + +C: + + package MyApp; + use Dancer2; + use MyApp::Admin + + hook before => sub { + var db => 'Users'; + }; + + get '/' => sub {...}; + + 1; + +C: + + package MyApp::Admin; + use Dancer2 appname => 'MyApp'; + + # use a lexical prefix so we don't override it globally + prefix '/admin' => sub { + get '/' => sub {...}; + }; + + 1; + +Without the appname directive, C would not have access +to variable C. In fact, when accessing C, the before hook would +not be executed. + +See L +for details. + +2. The following modules can be used to speed up an app in Dancer2: + +=over 4 + +=item * L + +=item * L + +=item * L + +=back + +They would need to be installed separately. This is because L does +not incorporate any C code, but it can get C-code compiled as a module. +Thus, these modules can be used for speed improvement provided: + +=over 4 + +=item * You have access to a C interpreter + +=item * You don't need to fatpack your application + +=back + +=head3 Plugins: plugin_setting + +C returns the configuration of the plugin. It can no +longer be called outside of C or C. + +=head3 Routes + +L requires all routes defined via a string to begin with a leading +slash C. + +For example: + + get '0' => sub { + return "not gonna fly"; + }; + +would return an error. The correct way to write this would be to use +C + +=head3 Tests + +Dancer2 recommends the use of L. + +For example: + + use strict; + use warnings; + + use Test::More tests => 3; + + use Plack::Test; + use HTTP::Request::Common; + + use Test2; + { package Test2; set apphandler => 'PSGI'; set log => 'error'; } + + test_psgi( Test2::dance, sub { + my $app = shift; + + my $res = $app->( GET '/' ); + + ok $res->is_success; + + is $res->code => 200, 'response status is 200 for /'; + + like $res->content => qr#Test2#, 'title is okay'; + } ); + +Other modules that could be used for testing are: + +=over 4 + +=item * L + +=item * L + +=back + +=head4 Logs + +C can no longer be used, as with L. Instead, +L could be used for testing, to capture all +logs to an object. + +For example: + + use strict; + use warnings; + use Test::More import => ['!pass']; + use Plack::Test; + use HTTP::Request::Common; + + { + package App; + use Dancer2; + + set log => 'debug'; + set logger => 'capture'; + + get '/' => sub { + debug 'this is my debug message'; + return 1; + }; + } + + my $app = Dancer2->psgi_app; + is( ref $app, 'CODE', 'Got app' ); + + test_psgi $app, sub { + my $cb = shift; + + my $res = $cb->( GET '/' ); + is $res->code, 200; + + my $trap = App->dancer_app->logger_engine->trapper; + + is_deeply $trap->read, [ + { level => 'debug', message => 'this is my debug message' } + ]; + }; + +=head3 Exports: Tags + +The following tags are not needed in L: + + use Dancer2 qw(:syntax); + use Dancer2 qw(:tests); + use Dancer2 qw(:script); + +The C command should be used instead. It provides a development +server and reads the configuration options in your command line utilities.