Express web application development using MayPole
JoE Jiang's Out: China Perl Association FPC (Foundation of Perlchina) Original: Rapid Web Application Deployment with MayPole Servers: Simon Cozens Original: http://www.perl.com/pub/a/2004/ 04/15 / maypole.html Table: April 22, 2004Perlchina Remind you: Please protect the author's copyright and maintain the crystallization of the author.
You have a database, you have a web server, you have a deadline.
Whether it is a new opportunity to design E-Commerce's window, or designed a new interface for the database of the personnel department, or even provide a method of tracking for American English slang usage, this is a matter, and The deadline is always yesterday.
In April this year, I developed a project called MayPole for the Perl Foundation. This project is a Web interface for the database, which is a web-based application, which is very fast.
No, I only need some very few Perl code, I can use MayPole to create an intranet entry, a database and display system for selecting menus and recipes, lyrical piano projecting software, an open source social network Site, a beer taste web database, all of which are completed within two weeks.
MayPole flexibility comes from three basics:
Clear concept distinguishing (sensitive) humanized default value easy to expand to clearly explain these three concepts, we immediately start watching a fast food type web application (a product catalog of online store) and see how long we can use Complete MayPole assembly.
Clear concept is distinguished by the start MayPole called Apache :: MVC, which reflects it is based on M-V-C mode. (I must change this name first because MayPole is not bundled on apache, and Apache :: MVC is also a soil name.) This model is used in many similar languages, such as Java Structs framework .
This design pattern is widely used in many graphics software. The core of this idea is to express and process data with a template. With a view to display data to the user, use a control class to ensure that the user event correctly drives other classes. activity. This metaphor is not fully mapped to web applications, but we can get an important concept. Andy Wardley is so explained:
Various forms of true power maps for M-V-C Web are conceptual clear separation. Place the database code in one place, apply the code in another, indicating the associated code in the third place. This allows you to adjust each element, don't worry about affecting other parts (of course, how you have to decompose the problem). This is usually a good attempt. By effective tandem input and output (control and view), MVC's by-products are the goal of achieving conceptual separation.
This is MayPole, he has a lot of database drivers, many front-end drivers and many templates representation. Normally, MayPole is always able to express your requirements in these areas, you can also focus on writing business logic. That's why MayPole enables you to develop the reason (most of you don't need any development).
So let's start, first of all, choose the technical element we want to use to implement the product database. We will actually use the most common models, views, and control configurations. MayPole provides a model-based model based on Template :: Toolkit's view and control based on Apache mod_perl. We will introduce these details of these things later, but now because this configuration is so universal, he is the default configuration, nor does it need to write any code to reach this. We also need a database. Our hypothetical customer name is iSellit, a computer device and software vendor. We need product tables, vendor tables, product catalog, and secondary categories. This is what the database looks like.
CREATE TABLE PRODUCT
ID INT NOT NULL AUTO_INCREMENT Primary Key,
Category Int,
Subcategory Int,
Manufacturer Int,
Part_number varchar (50),
Name varchar (50),
Cost Decimal (6, 2),
Description Text
);
CREATE TABLE MANUFACTURER
ID INT NOT NULL AUTO_INCREMENT Primary Key,
Name varchar (50),
URL VARCHAR (255),
Notes Text
);
CREATE TABLE CATEGORY
ID INT NOT NULL AUTO_INCREMENT Primary Key,
Name varchar (50)
);
CREATE TABLE SUBCATEGORY
ID INT NOT NULL AUTO_INCREMENT Primary Key,
Name varchar (50),
Category Integer
);
We assume that the data has been added to the database, but we also need salesperson to use a web interface to change the database content.
In order to use MayPole, we need a drive module. This is a very short module for defining our app. I said that the module may make you mistakenly think that it is related to writing code, but it is actually a hidden configuration. This is the drive module of our ISELLIT application. (This client can be called ISELLIT, but many years and perl modules have deal to make me get used to named lowercase letters)
Package isllit;
Use base 'apache :: mvc';
Use Class :: DBI: Loader :: Relationship;
ISELLIT-> Setup ("DBI: MySQL: ISELLIT");
ISELLIT-> config -> {uri_base} = "http:// localhost / isllit";
ISELLIT-> config -> {rows_per_page} = 10;
ISELLIT-> config -> {loadinger} -> Relationship ($ _) for
("A Manufacturer Has Products", "a category has produter,
"A Subcategory Has Products", "a category has subscategories");
1;
Ten lines, this is the size of the Maypole application you can look forward to. Let's explain it in our way:
Package isllit;
This is the name of our app, and we want to tell the name of the Perl service used by Apache on the web server. Use base 'apache :: mvc';
This tells you that we use Apache as the front end of MayPole so that we can start writing a mod_perl program.
Use Class :: DBI: Loader :: Relationship;
Now we use a Perl module I have written to coordinate the MAYPOLE drive class. This is what we can quite directly describe the relationship between the table of my database.
ISELLIT-> Setup ("DBI: MySQL: ISELLIT");
We let the iSellit connect to the database and analyze the status of the table and fields. And because we have not changed the default configuration of the class, you can definitely say we will use Class :: DBI and Template Toolkit. We can also say that if you use Apache :: MVC and DBix :: SearchBuilder and Html :: Mason, but did not say.
MayPole-based Class :: DBI class class Class :: DBI :: Loader to investigate the structure of the database, and map the product table inside to the ISELIT :: Product class, the same. You can read more information about Class :: DBI's tables and classes in Tony's article.
ISELLIT-> config -> {uri_base} = "http:// localhost / isllit";
Sometimes ISELLIT also needs to know that he is running in that URL address, which can be correctly generated in the application.
ISELLIT-> config -> {rows_per_page} = 10;
This line says we don't plan to display all products in a page, but most 10 per page.
ISELLIT-> config -> {loadinger} -> Relationship ($ _) for
("A Manufacturer Has Products", "a category has produter,
"A Subcategory Has Products", "a category has subscategories");
Now we use the ideal natural language to define the constraints in the relationship, a supplier has multiple products, and a classified directory has multiple products, so push.
Ten lines of code, what results do?
Sensitive default behavior MayPole's second foundation is the use of sensitive default behaviors. He uses a general template system to support the data in browsing and editing the library. In most cases, Web applications do not need to change its default behavior. In most cases, you can change a few of the templates. The essence is that the programmer can say that this is a web design group, then you don't have to do anything.
Ok, since we have installed the app and the default template, you can log in to the site with http: // localhost / isllit. It will be seen that this area is so much, and there is more exciting. If we click on the product list, you will go to this screen: Now we can already use these for sales staff, They don't have to do more work, you can add, modify, and delete products.
Similarly, if we click on the vendor's link in the product list, you will see a very passenger's page about the supplier. There are all their products. It can also be pushed according to this class. I want to write this ten lines of code. Then we let the web designers handle the template. MayPole Search Template in three different places, first look at the template with a class related to class, then take a look at the model related template, and finally use the default template of the template factory.
In this case, we can let them copy the Factory / View template into a supplier template and custom modification. We copy Factory / List to Product / List custom modification, and copy Factory / Header and Factory / Footer to the Custom / Directory, and then make them frames for each HTML page, so so. At present, I am not good at html design. This is also the reason I like MayPole. It let others help you solve the problem, but this also means that I can't show the realm of template. But there is an exception, I created a product / view with the following template.
[% Include header%]
[% Process macros%]
[% Maybe_Link_View (Product.Subcategory)%] div>
Div>