diff --git a/lib/Dancer2/Manual/Tutorial.pod b/lib/Dancer2/Manual/Tutorial.pod index 3ee1f484b..f9cd6a0df 100644 --- a/lib/Dancer2/Manual/Tutorial.pod +++ b/lib/Dancer2/Manual/Tutorial.pod @@ -1,1442 +1,485 @@ package Dancer2::Tutorial; # ABSTRACT: A step-by-step guide to get you dancing +# TODO: retab! =encoding utf8 =pod -=head1 Tutorial Overview +=head1 Dancer2 Tutorial -This tutorial is has three parts. Since they build on one another, each part is -meant to be gone through in sequential order. +The point is to gradually build up from simple examples and demonstrations +of techniques and end up with a working blog application. -B, the longest part of this tutorial, will focus on the basics of Dancer2 -development by building a simple yet functional blog app, called C, that -you can use to impress your friends, mates, and family. +# TODO: mention liteblog +# TODO: add notes, then share -In B, you'll learn about the preferred way to get your own web apps up -and running by using the C utility. We will take the script written in -Part I and convert it into a proper Dancer2 app, called C, to help you -gain an understanding of what the C utility does for you. +=head1 Introduction - the Danceyland Blog -Finally, in B, we give you a taste of the power of plugins that other -developers have written and will show you how to modify the C app to use -a database plugin. +=head2 What You'll Learn -This tutorial assumes you have some familiarity with Perl and that you know how -to create and execute a Perl script on your computer. Some experience with web -development is also greatly helpful but not entirely necessary. This tutorial is -mostly geared toward developers but website designers can get something out of -it as well since the basics of templating are covered plus it might be -good for a designer to have a decent idea of how Dancer2 works. +=head2 Prerequisites -=head1 Part I: Let's Get Dancing! +Linux, BSD, or Windows with WSL and a Linux distribution installed. -Part I covers many of the basic concepts you'll need to know to lay a good -foundation for your future development work with Dancer2 by building a simple -micro-blogging app. +Windows with Stawberry Perl may work, but is not guaranteed. -=head2 What is Dancer2? +You'll also need SQLite 3, installed via your operating system's package +manager. -Dancer2 is a micro-web framework, written in the Perl programming language, and is -modeled after a Ruby web application framework called L. +=head1 Setting Up Your Environment -When we say "micro" framework, we mean that Dancer2 aims to maximize your freedom -and control by getting out of your way. "Micro" doesn't mean Dancer2 -is only good for creating small apps. Instead, it means that Dancer2's primary -focus is on taking care of a lot of the boring, technical details of your app -for you and by creating an easy, clean routing layer on top of your app's -code. It also means you have almost total control over the app's functionality -and how you create and present your content. You will not confined to someone -else's approach to creating a website or app. +=head2 Installing Dancer2 -With Dancer2, you can build anything from a specialized content management -system to providing a simple API for querying a database over the web. But you -don't have to reinvent the wheel, either. Dancer2 has hundreds of plugins that -you can take advantage of. You can add only the capabilities your app needs to -keep complexity to a minimum. + cpan Dancer2 -As a framework, Dancer2 provides you with the tools and infrastructure you can -leverage to deliver content on the web quickly, easily and securely. The tools, -Dancer2 provides, called "keywords," are commands that you use to build your app, -access the data inside of it, and deliver it on the internet in many -different formats. +or -Dancer2's keywords provide what is called a B (DSL) -designed specifically for the task of building apps. But don't let the -technical jargon scare you off. Things will become clearer in our first code -example which we will look at shortly. + cpanm Dancer2 -=head3 Getting Dancer2 installed +=head2 Creating Your First App -First, we need to make sure you have Dancer2 installed. Typically, you will do -that with one of the following two commands: + dancer2 -a DLBlog - cpan Dancer2 # requires the cpan command to be installed and configured - cpanm Dancer2 # requires you have cpanminus installed +Output: -If you aren't familiar with installing Perl modules on your machine, you should -L. You may also want -to consult your OS's documentation or a knowledgeable expert. And, of course, -your search engine of choice is always there for you, as well. + + DLBlog + + DLBlog/.dancer + + DLBlog/config.yml + + DLBlog/cpanfile + + DLBlog/Makefile.PL + + DLBlog/MANIFEST.SKIP + + DLBlog/t + + DLBlog/t/002_index_route.t + + DLBlog/t/001_base.t + + DLBlog/environments + + DLBlog/environments/production.yml + + DLBlog/environments/development.yml + + DLBlog/bin + + DLBlog/bin/app.psgi + + DLBlog/views + + DLBlog/views/index.tt + + DLBlog/public + + DLBlog/public/dispatch.cgi + + DLBlog/public/404.html + + DLBlog/public/favicon.ico + + DLBlog/public/500.html + + DLBlog/public/dispatch.fcgi + + DLBlog/lib + + DLBlog/lib/DLBlog.pm + + DLBlog/views/layouts + + DLBlog/views/layouts/main.tt + + DLBlog/public/images + + DLBlog/public/images/perldancer.jpg + + DLBlog/public/images/perldancer-bg.jpg + + DLBlog/public/javascripts + + DLBlog/public/javascripts/jquery.js + + DLBlog/public/css + + DLBlog/public/css/style.css + + DLBlog/public/css/error.css -=head3 Your first Dancer2 "Hello World!" app + Your new application is ready! To run it: -Now that you have Dancer2 installed, open up your favorite text editor and copy -and paste the following lines of Perl code into it and save it to a file -called C: + cd DLBlog + plackup bin/app.psgi - #!/usr/bin/env perl - use Dancer2; + To access your application, point your browser to http://localhost:5000 - get '/' => sub { - return 'Hello World!'; - }; + If you need community assistance, the following resources are available: + - Dancer website: https://perldancer.org + - Twitter: https://twitter.com/PerlDancer/ + - GitHub: https://github.com/PerlDancer/Dancer2/ + - Mailing list: https://lists.perldancer.org/mailman/listinfo/dancer-users + - IRC: irc.perl.org#dancer - start; + Happy Dancing! -If you make this script executable and run it, it will fire up a simple, -standalone web server that will display "Hello World!" when you point your -browser to L. Cool! +Make sure to change to your new project directory to begin work: -B We want to emphasize that writing a script file like -this with a C command is B how you would typically begin writing a -Dancer2 app. Part II of this tutorial will show you the recommended approach -using the C utility. For now, we want to stay focused on the -fundamentals. + cd DLBlog -So, though our example app is very simple, there is a lot going on under the hood -when we invoke C in our first line of code. We won't go into the -gory details of how it all works. For now, it's enough for you to know that the -Dancer2 module infuses your script with the ability to use Dancer2 keywords for -building apps. Getting comfortable with the concept of keywords is probably the -most important step you can take as a budding Dancer2 developer and this -tutorial will do its best to help foster your understanding of them. +=head2 Understanding Directory Structure -The next line of code in our example (which spans three lines to make it more -readable) is the B. Let's examine this line closely, because -route handlers are at the core of how to build an app with Dancer2. - -The syntax of a Dancer2 C has three parts: +Your application directory now contains several subdirectories: =over -=item * -an B or B; in this example, we use the C keyword -to tell Dancer2 that this route should apply to GET http requests. C is the -first of many keywords that Dancer2 provides that we will cover in this tutorial. -Those familiar with web development will know that a GET request is what we use -to fetch information from a website. - -=item * -the B; this is the bit of code that appears immediately after -our C keyword. In this example it is a forward slash (C), wrapped in -single quotes, and it represents the pattern we wish to match against the URL -that the browser, or client, has requested. Web developers will immediately -recognize that the forward slash symbolizes the root directory of our website. -Experienced Perl programmers will pick up on the fact that the route pattern is -nothing more than an argument for our C keyword. - -=item * -the B; this is the subroutine that returns our data. More -precisely, it is a subroutine reference. The route action in our example returns -a simple string, C. Like the route pattern, the route action is -nothing more than an argument to our C keyword. - -Note that convention has us use the fat comma (C<< => >>) operator between the -route pattern and the action to to make our code more readable. But we -could just as well have used a regular old comma to separate these argument to -our C method. Gotta love Perl for its flexibility. - -=back - -So to put our route pattern in the example into plain English, we are telling -our app, "If the root directory is requested with the GET http method, send -the string 'Hello World!' back in our response." Of course, since this is a web -app, we also have to send back headers with our response. This is quitely taken -care of for us by Dancer2 so we don't have to think about it. - -The syntax for route handlers might seem a bit foreign for newer Perl developers. -But rest assured there is nothing magical about it and it is all just plain -old Perl under the hood. If you keep in mind that the keyword is a subroutine -(or more precisely, a method) and that the pattern and action are arguments to -the keyword, you'll pick it up in no time. Thinking of these keywords as -"built-ins" to the Dancer2 framework might also eliminate any initial confusion -about them. +=item bin -The most important takeaway here is that we build our app by adding route -handlers which are nothing more than a collection of, HTTP verbs, URL patterns, -and actions. +Contains the PSGI file that starts your application. Can be used for +other utilities and scripts for your application. -=head2 How about a little more involved example? +=item environments -While investigating some Python web frameworks like L -or L, I enjoyed the way they explained -step-by-step how to build an example application which was a little more involved -than a trivial example. This tutorial is modeled after them. +Configuration information. One file per environment (such as C +and C) -Using the -L -sample application as my inspiration (OK, shamelessly plagiarised) I -translated that application to the Dancer2 framework so I could better -understand how Dancer2 worked. (I'm learning it too!) +=item lib -So "dancr" was born. +Your core application code. This is where you'll put your models, +controllers, and other code. -dancr is a simple "micro" blog which uses the -L database engine for simplicity's sake. -You'll need to install sqlite on your server if you don't have it -installed already. Consult your OS documentation for getting SQLite -installed on your machine. +=item public -=head3 Required Perl modules +Contains static files such as images, CSS, and JavaScript. Also contains +instance scripts for CGI and FastCGI. -Obviously you need L installed. You'll also need the -L