Introduction to MVC Programming using Agavi, part 2nd: Adding Forms and database support using Agavi and doctrine 1
Brief introduction
In part 1th of this series, I introduced Agavi and explained some of the features that make Agavi suitable for building scalable, compliant Web applications. By using the sample application Web automobiles Sales Platform (WASP), I showed you the basics of creating a new Agavi project to help you understand Agavi's recommended file system layouts and familiarize yourself with Agavi's command-line build scripts. I also covered the underlying components of all Agavi applications-actions (actions), views, and routes (Routes)-and showed some built-in Agavi input validators.
Although Agavi used to provide static content, it is also a good way to handle more complex situations. In part 2nd, you'll examine Agavi's ability to handle complex content--in the next few sections, you'll learn how to receive, validate, and process input from Web Forms and connect Agavi applications to MySQL databases.
Create a form using Agavi
First, the index page for the Wasp application is quickly browsed (Figure 1):
Figure 1. Index page for wasp application
You have prepared the code in the previous article to handle two links to static content. Let's move on and start working on the "Contact Us" link. As the name suggests, this link points to a contact form through which you can contact a car dealer. The overall process of implementing this functionality is similar to the process of building staticcontentaction in the previous article.
Start the Agavi build script first and enter the following values:
shell> agavi action-wizarD
...
Module name: Default
Action name: Contact
Space-separated list of views to create for Contact [Success]: Input Error Success
This will create a new contactaction and 3 other views. You are already familiar with the two standard views of Contactsuccessview and Contacterrorview, which are displayed based on the success or failure of the operation. The third view Contactinputview is new, is the first view that the user sees, and displays a Web form that will accept user input.
Add a new route for the contactaction in the $wasp_root/app/routing.xml file, as shown in Listing 1:
Listing 1. Default/contactaction Routing Definition
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
xmlns="http://agavi.org/agavi/config/parts/routing/1.0">
<ae:configuration>
<routes>
...
<!-- action for contact form "/contact" -->
<route name="contact" pattern="^/contact$" module="Default"
action="Contact" />
</routes>
</ae:configuration>
</ae:configurations>