A Database-Driven Web Application in 18 Lines of Code

From zero to Web-based database application in eight easy steps.
Step 8: Give It a Go!

Restart Apache before accessing the Maypole application:

service httpd restart

I entered http://webmason.itcarlow.ie/Club/ into the Firefox location bar, and up popped Figure 1, which, although something, was not quite what I was expecting.

Figure 1. The Default Maypole Opening Screen

For starters, I was expecting to see some nice CSS output, not the plain HTML I was seeing. To fix this problem, I explored the default template files copied into the Web server during Step 6. By changing these, it is possible to alter the appearance of the application, without changing the source code to the application. The significance of that last sentence cannot be overstated. In essence, the way the application looks is controlled by the CSS templates. The way the application behaves is controlled by the code. The data used by the application is controlled by MySQL. All of this separation of duties makes for a very productive development environment, as changing one part of the application shouldn't adversely affect either of the others.

The templates live within a subdirectory called factory, located beneath the URL of the application, which is Club/ in this case. The factory templates are the Maypole defaults and are used unless overriding templates are found within another directory, called custom.

After creating the custom directory underneath the Club/ URL, I copied the header file from factory to custom and edited it with vi. I changed /maypole.css to read /club.css, in addition to replacing the “A poorly configured” message with a more appropriate description of the application. I also copied the frontpage file from factory to custom and edited it to use a better application description. Then, I changed the anchor tag within custom/frontpage to read “Work with the player data” as opposed to the default “List by player” text. With these changes made, I clicked the Reload button within Firefox, resulting in Figure 2, which—I think you'll agree—looks a whole lot better.

Figure 2. The Customized Soccer Club Opening Screen

Clicking on any of the menu options produces a beautifully formatted input screen, like those shown in Figures 3 and 4.

Figure 3. The Maypole Front End to the Squad Table

Figure 4. The Maypole Front End to the Player Table

Figure 4 shows the display after the entry of two fictitious players. Notice all the functionality provided for free. Tabs for each of the tables are located along the top of the display. Simply click on the tab to display that table's data. Each row of data has an associated edit and delete button. Click on any column heading to sort the display on the data in that column. Perform a search using the provided search form. Add more players using the add form. Notice the drop-down menus for the player's squad and medical condition. Click on the field and a drop-down box appears with the choices available to you. This bit of magic occurs because Maypole has been told that each player “has a” squad and “has a” condition. By default, Maypole uses the name data column in the referred to table to provide the data to these drop-down boxes.

And, that's it—a fully functioning Web interface to an underlying database, in eight easy steps.

Despite the fact that Maypole is quite new, an active community already has gathered around it. The mailing list recently split, one for developers and the other for users, and the Maypole Web site is now hosted by perl.org.

As I hope I've demonstrated, Maypole—once set up—is a breeze to use. Most of the guts of any Web application is provided for free. Adding additional functionality also is possible. Maypole is not stuck on MySQL either, as any SQL DBMS can be used. Refer to the articles and documentation referenced on the Maypole site for more details.

Resources for this article: /article/7964.

Paul Barry (paul.barry@itcarlow.ie) lectures at the Institute of Technology, Carlow in Ireland. Information on the courses he teaches, in addition to the books and articles he has written, can be found on his Web site, glasnost.itcarlow.ie/~barryp.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Please help

Justin Rickert's picture

I have tried to get the content of this article to work for three days now. I have finally got all the modules from CPAN installed. However when I try and use the App I get this message in the httpd error log:

failed to resolve handler `ClubDB':

and

Can't locate loadable object for module Apache::Constants

I installed Apache::Constants with
# perl -MCPAN -eshell
# install Apache::Constants

I have followed this article content and dont understant why it don't work.

Fights with Apache

Jason McKenna's picture

I'm running SuSE 9.1 with Apache 2 and had a hard time fighting to get things working. The one thing which took me the longest time to figure out was the Apache::MVC module does a check to see if you're running Apache 1 or 2. If 2 it requires one set of libraries, if 1 then a different set.

They both require Apache::Request, but as far I've been able to determine, this is not available for Apache 2:

if (APACHE2) {
require Apache2;
...
} else { require Apache }
require Apache::Request;

I seem to have fixed the problem by putting the "require Apache::Request" line inside the else block.

I don't know Perl or the Apache modules enough to say if this is a bug in MVC, or if my system is grossly misconfigured. Regardless, Maypole is working for me now, but I'm continuing to research. Any ideas?

Thanks!

Can't compile libapreq2 either

scottebetts's picture

Even though /sbin is in my path already. I'm on FC3 that was upgraded from FC2. I may try a clean new install in order to give this a whirl.

I couldn't get this tutorial

ElaineNormandy's picture

I couldn't get this tutorial to work until I made the following changes:

1. include /sbin in path when compiling libapreq2 module.

2. changed ClubDB.pm to have the following line:

ClubDB->setup( "dbi:mysql:CLUB", "manager", "passwordhere" );

instead of existing ClubDB->setup line.

Hope these tips help someone else save some hours.

Getting it to work

Mitch Kuppinger's picture

In addition to ElaineNormandy's suggestion which was key to getting this to work, I can offer these suggestions:

In ClubDB.pm since at line 9 we are in the ClubDB::Squad package definition, line 9 should be changed from "ClubDB::Player->untaint..." to "ClubDB::Squad->untaint...".

Be VERY carefull with your syntax in ClubDB.pm. For example, using curved braces, '(' and ')', instead of curly braces, '{' abd '}', after 'sub display_columns' causes the display of the associated table to be replaced by a message that the page can't be opened. The log ( logs/error.log) is minimally helpfull with this.

Be sure to restart Apache whenever ClubDB.pm is changed.

Paul Barry's article is thorough but does not walk you thru installing and setting up Apache and mod_perl, if you don't have them. This has to be done right to get the rest to work. Having the source code and all the resultant files from the build process (eg. apxs ) appears to be necessary to install libapreq2. My initial install of fc3 did not have these. It took a bit of poking around to get these required files in place.

All that said, I now have the tools in place to move some very usefull Interbase SQL databases to mysql and serve them up with browser technology to the various offices in our organization. Thanks to Paul Barry, Simon Cozens and Sebastion Riedel.

Getting it to work ... more

barryp's picture

Paul Barry's article is thorough but does not walk you thru installing and setting up Apache and mod_perl, if you don't have them.

I initially had a version of the article that covered installing Apache/mod_perl from source. However, in discussions with LJ's editor, it was decided that the article would be more useful if I targetted a distribution's build of Apache/mod_perl, the thinking being that if a distribution issued a security fix, it could be applied to the system as a result of the distribution's updating system. The idea was that building from source would mean that the user would then be taking on responsibility for updating Apache/mod_perl when security patches were issued. It was felt that people are busy enough, so we stuck with a distribution's Apache/mod_perl package.

My initial install of fc3 did not have these. It took a bit of poking around to get these required files in place.

As the last step of Part 1, I state: "be sure to install the following packages from your chosen distribution: httpd, httpd-devel, mod_perl, mod_perl-devel, mysql (client and server) and Perl.". With these packages installed, APXS and the like are installed for you. I did this on FC3 with no real issues.

Interbase SQL

You may wish to try hooking Maypole up to Interbase and avoid the step of moving your data to MySQL. Maypole is database-independent. Worth checking out.

Thanks to Paul Barry, Simon Cozens and Sebastion Riedel.

Oh my ... all I did was write the article. The "Simons" did all the real work and they deserve all the credit.

Thanks for the comments on the article. Glad you found it useful.

Regards.

Paul.

Paul Barry

getting Maypole (this tutorial) to work

barryp's picture

Unfortunately (or fortunately, depending on how you view things) Maypole and its API is constantly under development. The module is now at release 2.09, whereas my code worked under 2.04 and that was just a few short months ago. The best advice I can give web developers is to subscribe to the Mayole mailing list and to check any list archives. And -- yes -- getting Maypole to work is a challenge but, once it works, I think its worth the heartache.

Paul Barry

Paul Barry

Just the thing I've been pull

Anonymous's picture

Just the thing I've been pulling my hair out over the last 20 mins. Thanks!

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix