The Zend Framework Bootstrap made simple (wrap up)
The Zend Framework Bootstrap can be one of the key aspects of getting up and running quickly with the Zend Framework. Let’s make it easy.
Let me describe said situation for you; The user has a list of items and they’ve chosen to delete one. Given such a volatile action, you want them to doubly opt-in and confirm that they want to do this.
The last thing you want your user to feel, after they’ve clicked the delete link, is “NO WAAAAAAIIIIT! I didn’t mean to do that”. So the user clicks &’delete&’ and is directed to a confirmation page (which is rendered with Zend Form). There are two buttons on the page; the first is “Delete Item”, the second “Cancel”.
If the user clicks either button, the form submits to itself, where the value of the button clicked is determined. If the user clicks “Delete Item”, then the deletion is carried out, deleting the item from the datasource. If the user clicks “Cancel” the user is returned to the list of items they were previously viewing, the item is not deleted and the user is provided a message, confirming the non-deletion.
Let me describe said situation for you; The user has a list of items and they’ve chosen to delete one. Given such a volatile action, you want them to doubly opt-in and confirm that they want to do this.
The last thing you want your user to feel, after they’ve clicked the delete link, is “NO WAAAAAAIIIIT! I didn’t mean to do that”. So the user clicks &’delete&’ and is directed to a confirmation page (which is rendered with Zend Form). There are two buttons on the page; the first is “Delete Item”, the second “Cancel”.
If the user clicks either button, the form submits to itself, where the value of the button clicked is determined. If the user clicks “Delete Item”, then the deletion is carried out, deleting the item from the datasource. If the user clicks “Cancel” the user is returned to the list of items they were previously viewing, the item is not deleted and the user is provided a message, confirming the non-deletion.
But how do you determine which button was clicked by the user. Up until now, I’ve been using the getValues() or getValue() method on Zend_Form for retrieving form input. However, regardless of whether setIgnore is true or false on a form element, it’s not contained in that array. So how do you determine which button was clicked.
Well, there’s another set of methods available in Zend_Form; these are getUnfilteredValue() and getUnfilteredValues(). These do include the value of the form buttons. The value returned is the value property of the button, if it’s clicked or NULL if it wasn’t. Now what could be simpler? Not much to be honest. But what would a good post be without a working example? Not much either.
Ok now, have a look at the code below (sorry about the image instead of inline code - WordPress is playing up a bit at the moment). In it you see a simple controller action, which is aimed to delete some content. It does a standard check of whether the request was a POST or not and either displays the form, or attempts to validate the rendered content. No surprises here.
Then, if the form is a POST submission and the form is valid, I leave the validation requirements up to your hearts desire, we retrieve the value of the submit and cancel buttons, unimaginatively called &’submit&’ and &’cancel&'.
If the button is clicked, it will have a value, if not, it will be null. So, then we check if the submit button was clicked. If so, the user has confirmed the intent to delete the item, so we do that. If not, we then use the redirector action helper to redirect back to where we were - in this case, viewing a list of the existing items. I won’t get in to a discussion of redirect versus _forward. That’s already been done. But _redirector’s better!
Like all things in software, there’s many ways to accomplish them. But this one is simple and effective. However, there’s a number of ways to improve on this. Here’s a suggestion of the first three that come to mind:
What do you think? Where can it be improved? What would you do differently?
The Zend Framework Bootstrap can be one of the key aspects of getting up and running quickly with the Zend Framework. Let’s make it easy.
Recently I was asked how to rename a file with the Zend Framework that used a Zend Form and Zend File element. They key requirement was that it should not be a hack or a kludged solution. So I thought I’d write a quick post to provide a simple example on how it was achieved.
Ok, we’ve established that with the Zend Framework, we need to do a bit more work than some of the other frameworks to get up to speed - but that’s not necessarily a bad thing - right?! But it can be a bit tedious and it’s something as professional developers, we want to automate away. So we’ve been addressing in this series how to do just that with a custom, extendable bootstrap class.
In the first part of the series we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and modifying the default application bootstrap so that it extends from it and had a look at the first component - caching.
Then, in the second part of the series, we built on the foundation laid in part one by creating plugin resources for the routing table, application navigation and the database connections - these being some of the most common tasks, usually, associated with a web-based application.
In this, the third and final part of the series, I’m going to finish up by looking at application placeholders and surprise, no not logging as originally promised, but pagination. As an added extra, we’re going to be using a key feature of Zend Application to make it a breeze.
In the first part of the series, you’ll remember that we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and creating our custom bootstrap file that our application bootstrap will extend from.
After we did that, we put in the first but arguably the most important plugin resource – caching and stored it in the application registry. In this post we’re going to be building on that work and adding in three new plugin resources: routing, navigation and databases.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus