Dancer2
view release on metacpan or search on metacpan
lib/Dancer2/Manual/Tutorial.pod view on Meta::CPAN
use Dancer2;
get '/' => sub {
my @entries; # We'll populate this later
template 'index', { entries => \@entries };
};
get '/entry/:id' => sub {
my $id = route_parameters->get('id');
my $entry; # Populated from the database later
template 'entry', { entry => $entry };
};
get '/create' => sub {
template 'create';
};
post '/create' => sub {
my $new_id = 1;
redirect uri_for "/entry/$new_id"; # redirect does not need a return
};
get '/update/:id' => sub {
my $id = route_parameters->get('id');
template 'create';
};
post '/update/:id' => sub {
my $id = route_parameters->get('id');
redirect uri_for "/entry/$id";
};
get '/delete/:id' => sub {
my $id = route_parameters->get('id');
template 'delete', { id => $id };
};
post '/delete/:id' => sub {
my $id = route_parameters->get('id');
# Always default to not destroying data
my $delete_it = body_parameters->get('delete_it') // 0;
if( $delete_it ) {
# Do the deletion here
redirect uri_for "/";
} else {
# Display our entry again
redirect uri_for "/entry/$id";
}
};
true;
=head2 Adding Utility to our Views
This is all well and good, but it's not the most functional for our users.
A basic menu of available options would help users to understand what
actions can be performed in the Danceyland Blog.
=head3 Adding a Menu to our Layout
Let's edit our layout so users can see the same list of options across all
pages. By adding the menu once to the layout, we don't have to reproduce
this in the list, create, update, and delete templates.
Our menu will look like this:
<div id="menu">
<a href="<% request.uri_for('/') %>">List All Entries</a> |
<a href="<% request.uri_for('/create') %>">Create New Entry</a>
</div>
Now, let's add it to the top of our layout. The end result looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="<% settings.charset %>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title><% title %></title>
<link rel="stylesheet" href="<% request.uri_base %>/css/style.css">
</head>
<body>
<div id="menu">
<a href="<% request.uri_for('/') %>">List All Entries</a> |
<a href="<% request.uri_for('/create') %>">Create New Entry</a>
</div>
<% content %>
<div id="footer">
Powered by <a href="https://perldancer.org/">Dancer2</a> <% dancer_version %>
</div>
</body>
</html>
Refresh your browser to see the menu appear at the top of your page.
=head1 The Danceyland Database
We need some way to persist the blog entries, and relational databases
excel at this. We'll use SQLite for this tutorial, but you can use any
database supported by L<Dancer2::Plugin::Database> and L<DBI>.
L<SQLite|https://sqlite.org> is a lightweight, single file database that
makes it easy to add relational database functionality to a low-concurrency
web application.
=head2 Setting Up the Database
At minimum, we need to create a table to contain our blog entries. Create
a new directory for your database and SQL files:
$ mkdir db
Then create a new file, F<db/entries.sql>, with the following:
CREATE TABLE entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
summary TEXT NOT NULL,
content TEXT NOT NULL,
( run in 1.459 second using v1.01-cache-2.11-cpan-5623c5533a1 )