Xem mẫu

CHAPTER 5 • INTRODUCING THE ZEND FRAMEWORK 131 Listing 5-11. Registration E-mail in Production Mode Dear Jason, Welcome to the GameNomad community! To confirm your e-mail address, click on the following URL: http://www.gamenomad.com/gamers/verify/key/zs7lquz958qknj1hqq70gcg89egqsdf1 Questions? Comments? Contact us at support@gamenomad.com! Thank you, The GameNomad Team We can create these e-mail variations by passing configuration variables into a template (Listing 5-12). Listing 5-12. The Registration E-mail Template Dear {$firstName}, Welcome to the GameNomad community! To confirm your e-mail address, click on the following URL: {$this->config->website->url}/gamers/verify/key/{$registrationKey} Questions? Comments? Contact us at $this->config->email->support Thank you, The GameNomad Team The Zend Framework will be able to identify which set of configuration variables should be passed based on the value of APPLICATION_ENVIRONMENT, defined in the bootstrap.php file: defined(`APPLICATION_ENVIRONMENT`) or define(`APPLICATION_ENVIRONMENT`, `development`); Also in the bootstrap.php file you`ll need to execute two other commands: $config = new Zend_Config_Ini(`/home/webadmin/html/application/config.ini`, APPLICATION_ENVIRONMENT); Zend_Registry::set(`config`, $config); The first line identifies the location of the config.ini file, and identifies the defined application envi-ronment. The second line uses the Zend_Registry component to register the $config variable within the website`s registry, meaning it can later be retrieved using the following command: $this->config = Zend_Registry::get(`config`); You`ll need to ensure this command executes prior to retrieving the configuration variables from the Download at Boykma.Com 132 CHAPTER 5 • INTRODUCING THE ZEND FRAMEWORK config.ini file. Of course, the easiest way is to simply call the command directly prior to referencing one or more configuration variables, however as you`ll likely need to do so for multiple actions within a controller, later in this chapter I`ll show you an easy way to consolidate the number of times you need to make this call. The config.ini File The config.ini file (located in the application directory) is the central repository for your Zend Framework-powered website`s configuration data. You`re free to name the configuration parameters stored within anything you please, however they typically are identified using a dotted notation like so: database.params.username = gamenomad_prod When referring to the parameter within a script, you`ll prefix it with $this->config and converting the periods to right-facing arrows (->), like so: $this->config->database->params->username Notice how in the same config.ini file (Listing 5-13) these parameters are listed twice. Once under the section titled [production] and a second time under the section titled [development]. The Zend_Config component will choose the appropriate parameter according to the aforementioned AP-PLICATION_ENVIRONMENT setting. Listing 5-13. Sample config.ini file ; Production site configuration data [production] website.params.url = http://www.gamenomad.com database.params.host = mysql.gamenomad.com database.params.username = gamenomad_prod database.params.password = supersecret database.params.dbname = gamenomad_prod email.params.support = support@gamenomad.com ; Development site configuration data [development] website.params.url = http://beta.gamenomad.com database.params.host = mysql.gamenomad.com database.params.username = gamenomad_dev database.params.password = supersecret database.params.dbname = gamenomad_dev email.params.support = http://bugs.gamenomad.com Download at Boykma.Com CHAPTER 5 • INTRODUCING THE ZEND FRAMEWORK 133 Be sure to integrate this powerful component into your website at the earliest opportunity, as it will no doubt save both time and inconvenience as your site grows in size. Step #8. The init() Method To close out this chapter introducing the Zend Framework, I`d like to discuss a feature you`ll return to time and again, namely the init() method. Placed within a given controller, the init() method will execute prior to any other method found in the controller, giving you a convenient means for initial-izing variables and executing tasks which might need to occur prior to any other event related to that controller`s operation. For instance, if you know you`ll need access to the contents of the config.ini file within multiple actions you can add the following command to the init() method: $this->config = Zend_Registry::get(`config`); Listing 5-14 demonstrates this concept in action, retrieving the $config variable from the registry, and then using it within multiple actions. Listing 5-14. The init() method in action class MailController extends Zend_Controller_Action { function init() { $this->config = Zend_Registry::get(`config`); } public function registrationAction() { echo $this->config->mail->admin; } public function confirmationAction() { echo $this->config->db->username; } } Step #9. Creating Action Helpers Continuing the theme of figuring out ways to eliminate redundancy at every opportunity, the init() method is very useful if you want to share access to for instance the configuration file handle across multiple actions, but in all likelihood you`ll want access to the configuration file and certain other variables throughout your application. The result is a duplicated init() method within each control-ler, resulting in the need to touch every controller each time you want to make another variable glob-ally available, a clear violation of framework strategy. Or suppose you require a solution for generating random strings, which might be used when resetting Download at Boykma.Com 134 CHAPTER 5 • INTRODUCING THE ZEND FRAMEWORK passwords or within CAPTCHAs (see Chapter 7). It`s easy to insert such a method into a controller, however over time it`s likely the method will begin to multiply like rabbits as it`s needed within other parts of the application. One of the easiest solutions for remedying both of these problems involves creating an action helper. Much like view helpers facilitate the rendering of data within the view, action helpers facilitate the execution of certain tasks within controllers. Once in place, you`ll be able to refer to certain variables defined within the helper from any controller, not to mention call methods such as the aforementioned random string generator. Let`s conclude this chapter by creating an action helper which will make the Zend_Registry`s $config variable automatically available to all controllers. I`ll also show you how to create a method which can easily be executed within any action. Creating the InitializerAction Helper All action helpers inherit from the Zend_Controller_Action_Helper_Abstract class, so let`s begin by creating the action helper container: class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract { } Save this as Initializer.php and place it alongside your view helpers in the application/My/ helper directory. Because action helpers share many of the same characteristics of controllers, we`re going to create an init() method which will automatically execute when the action helper is invoked. As you`ll soon see, by registering the action helper within the bootstrap.php file, we will ensure this init() method is automatically invoked. Because the action helper is registered in the bootstrap.php file, it will execute prior to any controller action, and given the proper commands, will make certain variables available to that action. Here`s the init() method: public function init() { $controller = $this->getActionController(); $controller->config = Zend_Registry::get(`config`); } Thanks to the way the Zend Framework operates, the action helper knows which controller action has been invoked, meaning we can retrieve the executing controller using line 03. Line 04 will subse-quently create a variable named config which will be attached to the Zend_Registry`s configuration handle. Believe it or not, that`s all that`s required to assign variables which will then be made avail-able to the controller! But before you can put this action helper into service, you need to identify the location and prefix or your action helpers, just as you did previously during the introduction to the view helper. Download at Boykma.Com CHAPTER 5 • INTRODUCING THE ZEND FRAMEWORK 135 Loading the Action Helper To identify the location and prefix of your action helper, add the following line to your bootstrap. php file. Of course, you`ll need to change the values of each self-explanatory addPath() parameter as necessary: Zend_Controller_Action_HelperBroker::addPath( `C:\apache\htdocs\gamenomad.com\application\My\Helper`, `My_Action_Helper`); You can then invoke an action helper as needed using the following command $initStuff = Zend_Controller_Action_HelperBroker::getStaticHelper(`Initializer`); Note I`m just identifying the action helper by the name following the prefix. If you want the config variable to be available to all controllers, invoke the helper in your bootstrap.php file. Alternatively, if you just wanted these variables or other features to be available within select actions, you can call the helper as needed within the actions. The latter approach is particularly useful when you want to call a method, as is explained next. Calling an Action Helper Method I regularly use a method called generate_id() for generating random strings. Rather than define this method within every controller, it makes much more sense to define it within an action helper and call it as needed. This approach is not only useful in terms of removing code redundancy within a website, but also in terms of enhancing portability when you require similar features within other websites. You`ll create such a method within the action helper as you would any other class, just be sure to declare it as public so the method can be accessed outside of the class: public function generate_id() { return "superrandomstring"; } Once defined, you`ll be able to access the method from within any controller (after ensuring it`s loca-tion is registered within the bootstrap.php file), using the following bit of code: $initializer = Zend_Controller_Action_HelperBroker:: getStaticHelper(`Initializer`); $registrationKey = $initializer->generate_id(); Conclusion This chapter covered a significant bit of ground, introducing you to the Zend Framework`s fundamen-tal features. In the next chapter we`ll build upon what you`ve learned, introducing the crucial Zend_Db component, which makes database access using the Zend Framework a breeze. Download at Boykma.Com ... - --nqh--