CGI-Application-Structured
view release on metacpan or search on metacpan
Created MyApp1/script/create_dbic_schema.pl # IMPORTANT HELPER SCRIPT
Created MyApp1/script/create_controller.pl # ANOTHER IMPORTANT HELPER SCRIPT.
Created MyApp1/server.pl # SERVER USES YOUR CUSTOM DISPATCH.PM
Created MyApp1/MANIFEST
Created starter directories and files
Configure Your Database
Attanium is database centric in some sense and expects that you have a
database. Before running your app via server.pl you need to configure
your database access.
The example config is generated at MyApp1/config/config-dev.pl. The
contents are shown here.
use strict;
my %CFG;
$CFG{db_dsn} = "dbi:mysql:myapp1_dev";
$CFG{db_user} = "root";
$CFG{db_pw} = "root";
$CFG{tt2_dir} = "templates";
return \%CFG;
Using the root account is shown here as a worst-practice. You should
customize the file supplying the correct database dsn, user and
passwords for your database.
If you do not have a database and want to use an example see "Create
Example Database" below before continuing.
Generate A DBIx::Class Schema For Your Database
From your project root directory run the helper script to generate
DBIx::Class::Schema and Resultset packages. This will use the
configuration you supplied in config_dev.pl to produce a DB.pm in your
apps lib/MAINMODULE directory
~/dev/My-App1$ perl script/create_dbic_schema.pl
Dumping manual schema for DB to directory /home/gordon/dev/My-App1/script/../lib/My/App1 ...
Schema dump completed.
Given the example database shown below your resulting DBIx::Class
related files and folders would look like this:
~/dev/MyApp1$ find lib/MyApp1/ | grep DB
lib/MyApp1/DB
lib/MyApp1/DB/Result
lib/MyApp1/DB/Result/Orders.pm
lib/MyApp1/DB/Result/Customer.pm
lib/MyApp1/DB.pm
Run Your App
Before running your app you will need to export the CONFIG_FILE pointing
to your dev config file.
On linux you could use something like:
~/dev/MyApp1$ export CONFIG_FILE=/home/gordon/dev/MyApp1/config/config-dev.pl
On windows you could use something like:
C:\Users\gordon\dev\MyApp1: set CONFIG_FILE=C:\Users\gordon\dev\MyApp1\config\config-dev.pl
Run the server:
~/dev/MyApp1$ perl server.pl
access your default runmode at /cgi-bin/index.cgi
CGI::Application::Server: You can connect to your server at http://localhost:8060/
Open your browser and test at
http://localhost:8060/cgi-bin/index.cgi
Create a new Submodule
This is where the create_controller.pl helper script comes in very
handy. As an example we can generate a new module to interact with the
Orders table of the example database.
~/dev/MyApp1$ perl script/create_controller.pl --name=Orders
will try to create lib/MyApp1/C
Created lib/MyApp1/C/Orders.pm
will try to create template directory templates/MyApp1/C/Orders
Created templates/MyApp1/C/Orders
Created templates/MyApp1/C/Orders/index.tmpl
You can restart server.pl and view default output at:
http://localhost:8060/cgi-bin/orders
Add a new runmode to lib/MyApp1/C/Orders.pm to show the orders that we
have from the example database.
sub list: Runmode{
my $c = shift;
my @orders = $c->resultset("MyApp1::DB::Result::Orders")->all;
$c->tt_params(orders =>\@orders);
return $c->tt_process();
}
Then add a template for this runmode at
templates/MyApp1/C/Orders/list.tmpl with the following content:
<h1>Order List</h1>
<table>
<tr><th>Cust No</th><th>Order No</th></tr>
[% FOREACH order = orders %]
<tr>
<td>[% order.customer_id %]</td>
<td>[% order.id %]</td>
</tr>
[% END %]
</table>
Restart server.pl and visit page to see list of orders at:
http://localhost:8060/cgi-bin/orders/list
Creating The Example Database (if you don't already have one)
( run in 1.852 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )