The Askeet Tutorial
221 pages
English

The Askeet Tutorial

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
221 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

The Askeet Tutorial
symfony 1.0
This PDF is brought to you by
License: Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License
Version: askeet-1.0-en-2010-05-24 Table of Contents ii
Table of Contents
Day 1: starting up a project .............................................................................. 9
The challenge............................................................................................................. 9
The project................................................................................................................. 9
What for today ......................................................................................................... 10
Symfony installation ................................................................................................ 10
Project Setup ........................................................................................................... 11
Web service setup.................................................................................................... 11
Web server configuration ................................................................................................... 11
Declare the domain name 12
Test the new configuration................................................................................................. 12
IIS ............................................................................................................................ ...

Sujets

Informations

Publié par
Nombre de lectures 166
Langue English
Poids de l'ouvrage 2 Mo

Exrait

The Askeet Tutorial symfony 1.0 This PDF is brought to you by License: Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License Version: askeet-1.0-en-2010-05-24 Table of Contents ii Table of Contents Day 1: starting up a project .............................................................................. 9 The challenge............................................................................................................. 9 The project................................................................................................................. 9 What for today ......................................................................................................... 10 Symfony installation ................................................................................................ 10 Project Setup ........................................................................................................... 11 Web service setup.................................................................................................... 11 Web server configuration ................................................................................................... 11 Declare the domain name 12 Test the new configuration................................................................................................. 12 IIS ............................................................................................................................ 13 Subversion ............................................................................................................... 13 See you Tomorrow 15 Day 2: setting up a data model ....................................................................... 17 Previously on symfony ............................................................................................. 17 The project unveiled ................................................................................................ 17 Where to start? ........................................................................................................ 18 Data Model .............................................................................................................. 18 Relational model................................................................................................................. 18 schema.xml....................................................................................................................... 19 Object model build ............................................................................................................. 22 The database............................................................................................................ 22 Connection.......................................................................................................................... 22 Build ................................................................................................................................... 23 Test data access via a CRUD ................................................................................... 23 See you Tomorrow................................................................................................... 25 Day 3: dive into the MVC architecture ........................................................... 26 Previously on symfony ............................................................................................. 26 The MVC model ....................................................................................................... 26 Change the layout.................................................................................................... 27 A few words about environments ............................................................................ 28 Redefine the default homepage............................................................................... 29 Define test data........................................................................................................ 29 Create a batch to populate the database................................................................. 31 Batch skeleton .................................................................................................................... 31 Data import......................................................................................................................... 32 Launch the batch................................................................................................................ 32 Accessing the data in the model.............................................................................. 33 Modify the question/list template 34 Cleanup 35 ----------------- Brought to you by Table of Contents iii See you Tomorrow................................................................................................... 36 Day 4: refactoring ........................................................................................... 37 Previously on symfony ............................................................................................. 37 Show the answers to a question .............................................................................. 37 A quick look at the action 38 Modify theshowSuccess.php template........................................................................... 38 Add some new test data ..................................................................................................... 39 Modify the model, part I .......................................................................................... 40 Don’t repeat yourself ............................................................................................... 41 Modify the model, part II......................................................................................... 42 Add a field to theUser object model ................................................................................. 42 Modify thesave() method of theInterest object......................................................... 42 Secure the updating request with a transaction ................................................................ 43 Change the template .......................................................................................................... 44 Test the validity of the modification................................................................................... 45 Same for the answers .............................................................................................. 45 Routing .................................................................................................................... 47 Create an alternate version of the title .............................................................................. 47 Custom class....................................................................................................................... 48 Change the links to theshow action .................................................................................. 49 Changing the routing rules ................................................................................................ 49 See you Tomorrow................................................................................................... 50 Day 5: forms and pager................................................................................... 51 Previously on symfony ............................................................................................. 51 Login form ............................................................................................................... 51 Create theuser/login action.......................................................................................... 52 Create theloginSuccess.php template......................................................................... 52 Handle the login form submission...................................................................................... 53 Grant privileges.................................................................................................................. 54 Add theuser/logout action ............................................................................................ 54 Update the layout 55 Question pager ........................................................................................................ 55 Modify thequestion/list action ................................................................................... 55 Use a custom parameter .................................................................................................... 56listSuccess.php template........................................................................... 56 Add page navigation........................................................................................................... 57 Add a routing rule for the subsequent pages ..................................................................... 58 Refactoring .............................................................................................................. 58 Model.................................................................................................................................. 58 Templates ........................................................................................................................... 59 See you Tomorrow................................................................................................... 59 Day 6: security and form validation................................................................ 60 Previously on symfony ............................................................................................. 60 Login form validation............................................................................................... 60 Validation file ..................................................................................................................... 60 Error handling .................................................................................................................... 61 Template error helpers....................................................................................................... 61 Style errors......................................................................................................................... 62 Authenticate a user.................................................................................................. 62 Custom validator ................................................................................................................ 62 Password storage ............................................................................................................... 63 Add password in the test data............................................................................................ 63 ----------------- Brought to you by Table of Contents iv Custom validator ................................................................................................................ 62 Remove the code from the action....................................................................................... 66 Restrict access......................................................................................................... 66 How about a bit of refactoring?............................................................................... 67 See you Tomorrow................................................................................................... 68 Day 7: model and view manipulation.............................................................. 69 Previously on symfony ............................................................................................. 69 Prefactoring ............................................................................................................. 69 List of the recent questions ..................................................................................... 71 List of the recent answers 73 User profile.............................................................................................................. 75 Add a navigation bar................................................................................................ 76 Add the component in the layout ....................................................................................... 77 Define what action goes into the component ..................................................................... 77 Write thesidebar/default action and template ........................................................... 77 A little more view configuration .............................................................................. 78 Look at what we have done 80 See you Tomorrow................................................................................................... 81 Day 8: AJAX interactions................................................................................. 82 Previously on symfony ............................................................................................. 82 Add an indicator in the layout 82 Add an AJAX interaction to declare interest............................................................ 83 Caller .................................................................................................................................. 83 Result zone ......................................................................................................................... 85 Server action ...................................................................................................................... 85 Final test............................................................................................................................. 86 Add an inline ‘sign-in’ form...................................................................................... 86 Add a hidden login form to the layout................................................................................ 86 Have the form appear when a non-authenticated user clicks an interested link............... 87 Login the user..................................................................................................................... 87 See you Tomorrow................................................................................................... 88 Day 9: local improvements.............................................................................. 89 Previously on symfony ............................................................................................. 89 Allow rich text formatting on questions and answers ............................................. 89 Markdown........................................................................................................................... 89 Test Markdown text................................................................................................. 90 Markdown library............................................................................................................... 90 Extend the model................................................................................................................ 90 Override thesetBody method........................................................................................... 90 Update the test data........................................................................................................... 91 Modify the templates.......................................................................................................... 91 Hide allids.............................................................................................................. 92 Change the action 92 Change the model............................................................................................................... 93 Change the template 93 Add the routing rule 93 Routing .................................................................................................................... 93 See you Tomorrow................................................................................................... 95 Day 10: Alter data with Ajax forms ................................................................. 96 Previously on symfony ............................................................................................. 96 Add a new question.................................................................................................. 96 ----------------- Brought to you by Table of Contents v Restrict access to registered users .................................................................................... 96 TheaddSuccess.php template ........................................................................................ 97 Form validation .................................................................................................................. 98 Handle the form submission 98 Add a new answer.................................................................................................... 99 Add the AJAX form.............................................................................................................. 99 A little refactoring ............................................................................................................ 100............................................................................................. 101 Theanswer/add action................................................................................................... 101 Test it................................................................................................................................ 103 See you Tomorrow................................................................................................. 103 Day 11: syndication feed ............................................................................... 104 Previously on symfony ........................................................................................... 104 Popular questions feed .......................................................................................... 104 Link to the feed in the head 104 Install the plug-in ............................................................................................................. 104 Create the action.............................................................................................................. 105 Change the view configuration......................................................................................... 105 Write the template............................................................................................................ 106 Test it................................................................................................................................ 106 The magic 107 Interface improvements 108 Routing ............................................................................................................................. 108 RSS image ........................................................................................................................ 108 See you Tomorrow................................................................................................. 108 Day 12: Emails............................................................................................... 109 Previously on symfony ........................................................................................... 109 Password recovery 109 Password request form..................................................................................................... 109 Form validation ................................................................................................................ 110 Handling the request........................................................................................................ 110 Send an email 112 Email sending action 112 Mail template ................................................................................................................... 113 Alternate mail template.................................................................................................... 113 Configuration.................................................................................................................... 114 Test................................................................................................................................... 114 See you Tomorrow................................................................................................. 114 Day 13: Tags.................................................................................................. 115 Previously on symfony ........................................................................................... 115 TheQuestionTag class........................................................................................ 115 Schema update................................................................................................................. 116 Custom class..................................................................................................................... 117 Extend the model.............................................................................................................. 118 Add some test data ........................................................................................................... 118 Display the tags of a question................................................................................ 118 Extend model.................................................................................................................... 118 Modify the view ................................................................................................................ 119 Test it................................................................................................................................ 120 Display a short list of popular tags for a question................................................. 120 Extend the model 118 Modify the view 119 ----------------- Brought to you by Table of Contents vi Test................................................................................................................................... 123 Display the list of questions tagged with a word................................................... 123 Thetag/show action ....................................................................................................... 123 Extend the model.............................................................................................................. 118 Create the template.......................................................................................................... 124 Add thepage parameter in the routing rule.................................................................... 124 Test it................................................................................................................................ 120 See you Tomorrow................................................................................................. 124 Day 14: Tags, part II ..................................................................................... 126 Previously on symfony ........................................................................................... 126 Add tags to a question 126 The form ........................................................................................................................... 126 Autocomplete.................................................................................................................... 127 Remote form..................................................................................................................... 128 Test it................................................................................................................................ 129 Display the tag bubble 130 Extend theQuestionTagPeer class............................................................................... 130 Display a tag bubble......................................................................................................... 131 Style the tag list items...................................................................................................... 131 See you Tomorrow................................................................................................. 132 Day 15: Unit tests.......................................................................................... 133 Previously on symfony ........................................................................................... 133 Simple test............................................................................................................. 133 Unit tests in a symfony project .............................................................................. 134 Default unit tests .............................................................................................................. 134 Add a unit test .................................................................................................................. 135 Running unit tests ............................................................................................................ 136 The other way around ...................................................................................................... 136 When a test fails ............................................................................................................... 137 Simulating a web browsing session....................................................................... 138 ThesfTestBrowser object............................................................................................. 138 TheWebTestCase object................................................................................................. 139 Selenium........................................................................................................................... 140 A few words about environments .......................................................................... 144 See you Tomorrow 144 Day 16: Lazy day............................................................................................ 146 147 Day 17: API.................................................................................................... 148 Previously on symfony ........................................................................................... 148 The API .................................................................................................................. 148 HTTP Authentication ............................................................................................. 149 API response.......................................................................................................... 152 api/index action............................................................................................................ 152 questionSuccess.php template................................................................................... 153 Test it................................................................................................................................ 153 Integrating an external API 153 Paypal donation ..................................................................................................... 154 See you Tomorrow................................................................................................. 155 Day 18: Filters............................................................................................... 156 Previously on symfony ........................................................................................... 156 ----------------- Brought to you by Table of Contents vii Configurable feature.............................................................................................. 156 Create a filter......................................................................................................... 156 Get a permanent tag from the domain name......................................................... 157 Model modifications 158 Filter lists according to the permanent tag...................................................................... 159 Lists of tags for a question or a user................................................................................ 160 Append the permanent tag to new questions................................................................... 160 Server configuration.............................................................................................. 161 See you Tomorrow................................................................................................. 162 Day 19: Performance and cache 163 Previously on symfony ........................................................................................... 163 Load testing tools .................................................................................................. 163 JMeter............................................................................................................................... 164 Apache’s ab ...................................................................................................................... 165 Improve performances with the cache .................................................................. 166 Put the result of the action in the cache .......................................................................... 167 Build a staging environment ............................................................................................ 168 Put a template fragment in the cache .............................................................................. 169 Clear selective parts of the cache .................................................................................... 170 See you Tomorrow................................................................................................. 171 Day 20: Administration and moderation....................................................... 172 Previously on symfony ........................................................................................... 172 The expected result: what the client says ............................................................. 172 Stories .............................................................................................................................. 172 Story #1: Profile management ......................................................................................... 173 Story #2: Report of problematic questions or answers ................................................... 173 Story #3: Handling of problematic questions or answers ............................................... 173 Story #4: handling of problematic tags ........................................................................... 173 Story #4: Handling of problematic users......................................................................... 173 Is that all?......................................................................................................................... 174 Backend vs. enhanced frontend............................................................................. 174 The functionality: what the developers understand .............................................. 175 Implementation...................................................................................................... 176 New tables........................................................................................................................ 176 Cascade deletion .............................................................................................................. 177 Add links in the sidebar for users with credentials.......................................................... 178 AJAX report....................................................................................................................... 180 New action links for users with credentials ..................................................................... 182 Access restriction ............................................................................................................. 183 Newmoderator andadministrator actions............................................................... 184 Log deletions .................................................................................................................... 184 See you Tomorrow................................................................................................. 185 Day 21: Search engine .................................................................................. 187 Previously on symfony ........................................................................................... 187 How to build a search engine? .............................................................................. 187 Word index 188 Index table........................................................................................................................ 188 Splitting phrases into words............................................................................................. 189 Stop words 189 Stemming ......................................................................................................................... 190 Giving weight to words..................................................................................................... 190 Updating the index ........................................................................................................... 191 ----------------- Brought to you by Table of Contents viii Test the index builder....................................................................................................... 193 The search function ............................................................................................... 194 AND orOR? ........................................................................................................................ 194 The search method ........................................................................................................... 194 The search form................................................................................................................ 196 Display the search result.................................................................................................. 198 See you Tomorrow................................................................................................. 199 Day 22: Transfer to production..................................................................... 200 Previously on symfony ........................................................................................... 200 Synchronization ..................................................................................................... 200 Good practices.................................................................................................................. 200 Thesymfony sync command......................................................................................... 201 Ignoring irrelevant files.................................................................................................... 201 Production server configuration............................................................................ 202 Installing symfony in a production server........................................................................ 202 Access to the symfony commands in production.............................................................. 203 Web command 204 Upgrading your application................................................................................... 205 Show unavailability notice................................................................................................ 205 Use two versions of your application ............................................................................... 206 Switching databases......................................................................................................... 206 See you Tomorrow................................................................................................. 207 Day 23: Internationalization......................................................................... 208 Previously on symfony ........................................................................................... 208 Localization............................................................................................................ 208 Content............................................................................................................................. 208 Look and feel .................................................................................................................... 209 Language-dependent functions ........................................................................................ 209 Database content.............................................................................................................. 209 Internationalization ............................................................................................... 209 Set the culture.................................................................................................................. 209 Dates, Times, Numbers, Currency, Measurements.......................................................... 210 Interface translation......................................................................................................... 211 Automated translation...................................................................................................... 213 Don’t forget ...................................................................................................................... 213 See you Tomorrow................................................................................................. 215 Day 24: What’s next?..................................................................................... 216 Previously on symfony ........................................................................................... 216 Use it 216 Install it.................................................................................................................. 216 Download.......................................................................................................................... 217 Documentation ................................................................................................................. 217 File structure.................................................................................................................... 217 Data model ....................................................................................................................... 218 Contribute to it ...................................................................................................... 218 Acknowledgements................................................................................................ 219 See you soon.......................................................................................................... 219 ----------------- Brought to you by Day 1: starting up a project 9 Day 1 starting up a project The challenge The symfony advent calendar is a set of 24 tutorials, published day-by-day between December 1st and Christmas. That’s right, every day including week-ends, a new tutorial will be published. Each tutorial is meant to last less than one hour, and will be the occasion to see the ongoing development of a web 2.0 application, from A to Z. For Christmas time, the resulting application will be put online, and the source code made open source. This application will be usable, interesting, useful, and fun. Twenty-four times less than one hour equals less than a day, and that’s exactly how long we think that a developer needs to learn symfony. Every day, new features will be added to the application, and we’ll take advantage of this development to show you how to take advantage of symfony’s functionality as well as good practices in symfony web development. Every day, you will realize how fast and efficient it is to develop a web app with symfony, and you will want to know more. Considering that it wouldn’t be enough of a challenge with just that, and also because we are lazy folks, we have no plans for the 21st day - winter time. The feature that the community will require the most will be added to the application that day, without preparation, and we’ll make it work. It will be the get-a-symfony-guru-for-a-day day. The project The application to be designed could have been a trivial “show-and-tell” application, like a to- do list, a phone book, or a bookstore. But we want to use symfony on an original project, something useful, with numerous features and an important size. The goal is really to prove that symfony can be used in complex situations, to develop professional applications with style and little effort. We also hope that lots of people will actually use the application, in order to show that a symfony website can support an important load. That’s why the application needs to bring an actual service, and to answer an existing need - or to create a new one. The launch of the website will be a live stress test; this also means that we will need you, humble readers, to digg/bookmark/blog the site and talk about it in real life to check how many visits it can support. The content of the project will be kept secret for another day. We still have much to do today without describing a full-featured web 2.0 application. This should give you some time to argue and launch additional hypothesis. However, we need a name, so let’s call it: askeet. ----------------- Brought to you by Day 1: starting up a project 10 What for today The objective of the day is to display a page of the application in a web browser, and to setup a professional development environment. This includes installation of symfony, creation of an application, web server configuration, and setup of a source version control system. It should be easy for those who already followed the previous tutorials, and not very hard for others. And everyone should learn something new. We’ll assume that you use a Unix-like system with Apache, MySQL and PHP 5 installed. If you run a Windows system, don’t panic: it will also work fine, you’ll just have to type a few commands in thecmd prompt. Unix shell commands can come in handy in a Windows environment. If you would like to 1use tools liketar,gzip, orgrep on Windows you can install Cygwin . The official docs are 2a little sparse, so a good installation guide can be found here . The adventurous may also 3like to try Microsoft’s Windows Services for Unix . Symfony installation 4The simplest way to install symfony is to use the PEAR package. However, to be able to use channels - and access the channel - you need to upgrade to PEAR 1.4.0 or greater (unless you run PHP 5.1.0, which includes PEAR 1.4.5): Listing $ pear upgrade PEAR 1-1 5if you experience any problem while using PEAR, refer to the Running symfony book chapter. Now you can add the ‘symfony’ channel: Listing $ pear channel-discover pear.symfony-project.com 1-2 You are ready to install the latest stable version of symfony together with all its dependencies: Listing $ pear install symfony/symfony 1-3 If a specific version is desired, say 1.0.11, it can be specified as follows: Listing $ pear upgrade symfony/symfony-1.0.11 1-4 Check that symfony is installed by using the command line to check the version (note capital - V): Listing $ symfony -V 1-5 1. http://cygwin.com/ 2. http://www.soe.ucsc.edu/~you/notes/cygwin-install.html 3. http://technet.microsoft.com/en-gb/interopmigration/bb380242.aspx 4. http://pear.php.net 5. http://www.symfony-project.com/book/1_0/03-Running-Symfony ----------------- Brought to you by
  • Accueil Accueil
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • BD BD
  • Documents Documents