PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

Simplify PHP Development with WASP
Pages: 1, 2, 3, 4, 5

Testing the Install

Before you go on, it's important to make sure everything has been installed correctly and that the controllers and views are operating. By default, the application and each module will draw a dummy page with its name if you navigate to them right now.

Start your web server and browse to http://localhost/Todo/ (Note: link will only work if you've followed the tutorial this far.) You should see something like Figure 1.

TodoMain index page
Figure 1. TodoMain index page

You may get the message:

can not write to 'compileDir', which is
    '/path/to/wasp/install/directory/templates_c/'
      Please give write and enter-rights to it

If so, make sure that you have a templates_c directory in your project directory and that it has the correct permissions for your web server to read and write to it. This is the compiled templates directory where Flexy writes all of the compiled PHP code it generates from the .chunk files.

Now point your browser at http://localhost/Todo/Entry/ and make sure it also loads properly.

Entry Module

Now it's time to create the code for the Task Entry page. This is where you'll enter the tasks for your list.

Here's the HTML to use for the template located in Todo/Entry/templates/index.chunk:

 <html>
  <body>
    <form name="entry" method="post">
      <h3>Create Entry</h3>
      <p>
        Name:<br/>
        <input type="text" name="Name"/>
      </p>
      <p>
        Date Due (format mm/dd/yyyy):<br/>
        <input type="text" name="Due"/>
      </p>
      <input type="submit" name="Add" value="Add"/>
    </form>
  </body>
 </html>

Because the Task entries will appear on the Main Todo page, you don't have to even include any data display code here. This page is just a simple form with two text inputs and a submit button.

That's all you have to do for the Entry page. The next time you visit the /Todo/Entry URL you should see Figure 2.

the create entry page
Figure 2. The create entry page

Now that you have the page drawing correctly, plug in the events. The only event you have to handle on this page is the Add button press. You'll need to modify the event handling code to do this. To stick to convention, handle all form processing (and other Event processing) in the handleEvents() method in the Chunk class.

Here's the handleEvents() method for Todo/Entry/EntryIndexPage.php:

protected function handleEvents()
   {
     //Check for Add button presses
     if (Request::getParameter('Add') != null)
     {
         $oTask = new TaskWrapper();
         $oTask->fillFromRequest();
         $oTask->save();
         $this->redirect('../');
     }
   }

The code:

//Check for Add button presses
if (Request::getParameter('Add') != null)
{
  $oTask = new TaskWrapper();
  $oTask->fillFromRequest();
  $oTask->save();
  $this->redirect('../');
}

handles the event of the user submitting the form on the Entry page by pressing the Add button. When this happens, you want to create a new Task with a name and due date specified in the text box on the page. Because the text boxes named Name and Due correspond to the column names in the task table, the lines:

$oTask = new TaskWrapper();
$oTask->fillFromRequest();

construct a new TaskWrapper object and fill it from data submitted in the form. In this case, the Name and Due fields are the only data that applies because this table is small, but you can imagine how easily you could save large amounts of data in this way. Alternately, you could parse the request and manually set the fields in the $oTask object, but in most cases you shouldn't have to. The line:

$oTask->save();

does exactly what you think it should do. Because the TaskWrapper object has no primary key set yet, it saves a new record to the database. If you had constructed it with:

$oTask = new TaskWrapper(12);

and then called save(), it would have updated an existing record with the primary key of 12.

Notice the call to $this->redirect(). This takes you to the main page when you submit an entry so you can see your work. As you haven't written the main page, you won't be able to see your saved tasks just yet.

Pages: 1, 2, 3, 4, 5

Next Pagearrow




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com