LoudBitecom
Sign Up | View All Artists | Add Artists | My Store
Add Artist
Artist Name: Genre:
Add to Favorite List Rate: l<~|2<~|3i~|4<~|5i~
Add Artist |
Figure 4-2. Adding a favorite artist
You need a way to process the data the user submits using the add artist page. Create an additional action, saveArtistAction(), in the ArtistController.php file, as shown in Listing 4-4.
Listing 4-4. ArtistController.php: New Additions
* Save the Artist entered by the user.
public function saveArtistAction(){
//Initialize variables
$artistName = $this->_request->getPost('artistName'); $genre = $this->_request->getPost('genre'); $rating = $this->_request->getPost('rating'); $isFav = $this->_request->getPost('isFav');
//Validate
//Save the input into the DB
The new action, saveArtistAction(), is invoked after the user has submitted the form rendered by newAction() in ArtistController. The action initializes four variables ($artistName, $genre, $rating, and $isFav) that have been submitted using a POST request. Using the controller's request object, you fetch the POST data using the getPost() method by passing in the name of the POST parameter to fetch. After you initialize the data, you can validate the data using Zend_Validate (covered later in this chapter) and save it to the database, among other operations. A comment is placed that acts as a placeholder for validation and persisting for now.
If you have already filled out the form and tried submitting it, you will receive a similar error message:
script 'artist/save-artist.phtml' not found in path (C:\Apache\htdocs\loudbite\application\views\scripts\)
If you received such an error, don't panic; it's simply Zend Framework telling you that it did not find a view to render. By default, it looks for the file under the views/scripts/<NAME OF CONTROLLER> directory, even if you did not explicitly ask the action to render a view.
Let's go ahead and create the save-artist.phtml file, which is a thank you page for the action and save it in the directory views/scripts/artist. The file should contain the XHTML shown in Listing 4-5.
Listing 4-5. save-artist.phtml
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<?php echo $this->headTitle('LoudBite.com - Add Artist'); ?>
<?php echo $this->render('includes/header.phtml') ?>
Thank you. The artist has successfully saved.<br/> <a href='new'>Add another artist</a> </p> </body> </html>
Reload the form and resubmit it. The error is gone, and you should now be able to submit the form without any problem.
You'll now create a view object and not use the autoinitialized Zend_View object created for you. To demonstrate the feature, you'll create the controller, action, and view for the account update page. The page displays all the information the user provided during the sign-up process and allows users to update the information. Add the updateAction() method to the AccountController.php file as shown in Listing 4-6.
Listing 4-6. AccountController.php: New Additions
* Update the user's data.
public function updateAction() {
//Check if the user is logged in //Get the user's id
//Get the user's information
//Create the Zend_View object $view = new Zend_View();
//Assign variables if any
$view->setScriptPath("<ABSOLUTE PATH TO VIEW DIRECTORY>"); $view->render("update.phtml");
Beginning with the placeholder comments, check whether the user is authenticated. If so, you retrieve the user ID from the session and the data stored in the database using the user ID. You then prepare the view.
You instantiate the Zend_View object and set the path that Zend Framework will use when looking up views by calling setScriptPath(). The setScriptPath() method accepts a string parameter containing the path to the directory containing your views. The last line instructs the Zend_View object to render the specified script. In this case, it's the update.phtml view, shown in Listing 4-7.
Listing4-7. update.phtml
<?php echo $this->doctype('XHTML1_STRICT'); ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" <head>
<?php echo $this->headTitle('LoudBite.com - Update Info'); </head> <body>
<?php echo $this->render('includes/header.phtml') ?>
<h3>Update My Profile</h3> <form action="saveprofile" method="POST"> <table width="500" border="0"> <tr>
<td>Username: </td> <td><input type="text" name="username"
value="<?php echo $this->username?>" /></td> </tr> <tr>
<td><input type="text" name="email"
value="<?php echo $this->email?>" /></td> </tr> <tr>
<td><input type="password" name="password" value="<?php echo $this->password?>" /></td> </tr> <tr>
<input type="submit" value=" Update my account! " /> </td> </tr> </table> </form> </body> </html>
Listing 4-7 demonstrates the form used for sign-up. The only difference is the value attribute in each input field. This new addition allows you to add the user's information as soon as the page is loaded.
Load the URL http://localhost/account/update in your browser, and you should see Figure 4-3.
LoudBitexom
Sign Up | View All Artists | Add Artists | My Store
Update My Profile
Usemame: Email: Password: U p d ate rny acco u nt!
Figure 4-3. Updating the profile
So if it's this easy to create and use the view, what is the big deal about using views? In other words, why devote an entire chapter to the Zend_View component?
Post a comment