1. Trang chủ
  2. » Công Nghệ Thông Tin

Rails for PHP Developers pot

409 728 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 409
Dung lượng 6,71 MB

Nội dung

www.it-ebooks.info What readers are saying about Rails for PHP Developers This is a thorough and approachable introduction to Ruby and Rails for PHP programmers from fellow developers who are well-versed in both Ruby and PHP. Paul M. Jones Lead Developer on the Solar Framework for PHP As a PHP developer, I found the book focused well on the transition from coding PHP to coding Ruby (and Rails) and that it gave great examples of translating common PHP idioms to Ruby. Matthew Weier O’Phinney PHP Developer and Zend Framework Core Contributor The quality of the writing is superb, t he challenges and examples are engaging, and the PHP to Ruby information is a valuable resource. The exercises are nice, are short, and follow the topic well, giving readers some creative time between each chapter. Mislav Marohni ´ c Prototype JavaScript Framework Core Developer This is an enjoyable book packed with great information and usable examples. I like the organization of the book and the gentle, infor- mal voice with which the authors cover many complex topics. It’s easy to read, yet it has plenty of substance and depth to give the reader a great introduction to Rails. Bill Karwin MySQL Guild and Former Zend Framework Project Leader www.it-ebooks.info Rails for PHP Devel opers Der ek DeVries Mike Nabe rezny The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those desi gnations appear in this book, and The Pragmatic Programmers, LLC wa s aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more infor mation, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Derek DeVries and Mike Naberezny. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical , photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-04-2 ISBN-13: 978-1-9343560-4-3 Printed on acid-free paper with 50% recycled, 15% post-consumer content. First printing, January 2008 www.it-ebooks.info Contents Acknowledgments 10 Preface 11 What Rails Off ers . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Who Should Read This Book . . . . . . . . . . . . . . . . . . . 12 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 PHP and Rails: A Personal View . . . . . . . . . . . . . . . . . . 13 About the Code Examples . . . . . . . . . . . . . . . . . . . . . 14 About the Environment Used . . . . . . . . . . . . . . . . . . . 14 Version Requirements . . . . . . . . . . . . . . . . . . . . . . . 15 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . 15 I From PHP to Rails 17 1 Getting Started with Rails 18 1.1 Rails as an Extension of Ruby . . . . . . . . . . . . . . 18 1.2 The Components of Rails . . . . . . . . . . . . . . . . . 19 1.3 Opinionated Software . . . . . . . . . . . . . . . . . . . 20 1.4 The MVC Pattern and Rails . . . . . . . . . . . . . . . . 22 1.5 Installing Ruby and Rails . . . . . . . . . . . . . . . . . 24 1.6 Creating a Rails App . . . . . . . . . . . . . . . . . . . . 25 1.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 42 1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 Beginning Ruby Code 43 2.1 Seeing Ruby as a General-Purpose Language . . . . . 43 2.2 Interacting with Ruby . . . . . . . . . . . . . . . . . . . 45 2.3 Objectifying Everything . . . . . . . . . . . . . . . . . . 47 2.4 Accepting Ruby’s Object World . . . . . . . . . . . . . . 48 2.5 Assigning to Variables . . . . . . . . . . . . . . . . . . . 50 2.6 Writing Methods and Passing Parameters . . . . . . . . 53 2.7 Controlling Program Flow . . . . . . . . . . . . . . . . . 57 www.it-ebooks.info CONTENTS 6 2.8 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . 59 2.9 Understanding Blocks . . . . . . . . . . . . . . . . . . . 65 2.10 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 70 2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3 Embracing the Ruby Philosophy 72 3.1 Thinking in Objects . . . . . . . . . . . . . . . . . . . . 72 3.2 Understanding Attributes . . . . . . . . . . . . . . . . . 75 3.3 Method Visibility . . . . . . . . . . . . . . . . . . . . . . 77 3.4 Understanding Typing . . . . . . . . . . . . . . . . . . . 79 3.5 Implementing Interfaces with Mixins . . . . . . . . . . 84 3.6 Organizing Code with Namespaces . . . . . . . . . . . . 88 3.7 Overriding Operators . . . . . . . . . . . . . . . . . . . . 91 3.8 Reopening Classes . . . . . . . . . . . . . . . . . . . . . 93 3.9 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 95 3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 II Building a Rails Application 96 4 Modeling the Domain 97 4.1 Defining Requirements . . . . . . . . . . . . . . . . . . 98 4.2 Using the Database . . . . . . . . . . . . . . . . . . . . 101 4.3 Creating the Application . . . . . . . . . . . . . . . . . . 104 4.4 Generating the First Model . . . . . . . . . . . . . . . . 106 4.5 Building Database Tables . . . . . . . . . . . . . . . . . 108 4.6 Employing ActiveRecord . . . . . . . . . . . . . . . . . . 113 4.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 118 4.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5 Working with Controllers and Views 120 5.1 Identifying Resources . . . . . . . . . . . . . . . . . . . 120 5.2 Creating Contr ollers . . . . . . . . . . . . . . . . . . . . 122 5.3 Routing Requests . . . . . . . . . . . . . . . . . . . . . . 125 5.4 Retrieving Meeting Data . . . . . . . . . . . . . . . . . . 129 5.5 Viewing Meetings . . . . . . . . . . . . . . . . . . . . . . 131 5.6 Adding Links . . . . . . . . . . . . . . . . . . . . . . . . 132 5.7 Creating New Meetings . . . . . . . . . . . . . . . . . . 138 5.8 Redirection and Flash Data . . . . . . . . . . . . . . . . 143 5.9 Administrating Meetings . . . . . . . . . . . . . . . . . . 145 5.10 Separating Public Files . . . . . . . . . . . . . . . . . . 153 5.11 Adding a Layout . . . . . . . . . . . . . . . . . . . . . . 155 www.it-ebooks.info CONTENTS 7 5.12 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 158 5.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6 Validating and Testing Models 161 6.1 Validating Model Data . . . . . . . . . . . . . . . . . . . 161 6.2 Using Rails Environments . . . . . . . . . . . . . . . . . 164 6.3 Testing Our Models . . . . . . . . . . . . . . . . . . . . . 165 6.4 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 171 6.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7 Authenticating Users 173 7.1 Migrating to a More Secure User . . . . . . . . . . . . . 173 7.2 User Registration . . . . . . . . . . . . . . . . . . . . . . 175 7.3 Viewing and Editing Users . . . . . . . . . . . . . . . . 186 7.4 Restoring Sessions . . . . . . . . . . . . . . . . . . . . . 192 7.5 Logging In . . . . . . . . . . . . . . . . . . . . . . . . . . 195 7.6 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 200 7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 8 Defining Associations 202 8.1 Connecting Presentations . . . . . . . . . . . . . . . . . 202 8.2 Testing Associations . . . . . . . . . . . . . . . . . . . . 205 8.3 Integrating Presentations into Meetings . . . . . . . . . 207 8.4 Routing Presentations . . . . . . . . . . . . . . . . . . . 208 8.5 The Presentation Controller . . . . . . . . . . . . . . . . 210 8.6 Spring Cleaning . . . . . . . . . . . . . . . . . . . . . . . 215 8.7 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 221 8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 9 Preparing to Launch 223 9.1 Adding the Home Page . . . . . . . . . . . . . . . . . . . 223 9.2 Securing Our Actions . . . . . . . . . . . . . . . . . . . 226 9.3 Protecting from Mass Assignment . . . . . . . . . . . . 232 9.4 Caching the Pages . . . . . . . . . . . . . . . . . . . . . 234 9.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 240 9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 10 Deploying the Application 242 10.1 Choosing a Host . . . . . . . . . . . . . . . . . . . . . . 243 10.2 The Production Environment . . . . . . . . . . . . . . . 245 10.3 Preparing Our Application . . . . . . . . . . . . . . . . . 246 10.4 Preparing Our Deployment Ser ver . . . . . . . . . . . . 248 www.it-ebooks.info CONTENTS 8 10.5 Launching the Application . . . . . . . . . . . . . . . . 253 10.6 Enhancing Performance . . . . . . . . . . . . . . . . . . 256 10.7 Scaling Your Application . . . . . . . . . . . . . . . . . . 260 10.8 Chapter Review . . . . . . . . . . . . . . . . . . . . . . . 261 10.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 III PHP to Ruby at a Glance 263 11 PHP to Ruby Basics Reference 264 11.1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . 264 11.2 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . 266 11.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 11.4 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . 292 11.5 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 294 11.6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 296 11.7 Control Structures . . . . . . . . . . . . . . . . . . . . . 305 12 PHP to Ruby Advanced Reference 316 12.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 12.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 318 12.3 Classes and Objects . . . . . . . . . . . . . . . . . . . . 326 12.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 354 12.5 References . . . . . . . . . . . . . . . . . . . . . . . . . . 355 12.6 External Libraries and Packages . . . . . . . . . . . . . 357 12.7 Documenting Code . . . . . . . . . . . . . . . . . . . . . 359 13 PHP to Rails Reference 371 13.1 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 371 13.2 $_GET/$_POST . . . . . . . . . . . . . . . . . . . . . . . 372 13.3 $_FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 13.4 $_SER VER . . . . . . . . . . . . . . . . . . . . . . . . . . 376 13.5 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 13.6 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 13.7 Headers and Redirection . . . . . . . . . . . . . . . . . 380 13.8 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 13.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 386 13.10 Accessing the Database . . . . . . . . . . . . . . . . . . 388 13.11 E mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 13.12 Testing Rails Code . . . . . . . . . . . . . . . . . . . . . 391 13.13 R ails Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . 396 www.it-ebooks.info CONTENTS 9 Bibliography 399 Index 400 www.it-ebooks.info Acknowled gm ents Derek would like to thank Melissa, daVinci, and h i s new baby girl, Sevilla, who was born during the writing of this preface. Mike would like to thank Kathy for her support and his parents for buying his first computer, the Commodore 64. We’d like to thank our reviewers: Bill Karwin, Mislav Marohnic, Tim Fletcher, Paul M. Jones, Matthew Weier O’Phinney, Dallas DeVries, Laura Thomson, and Chuck Hagenbuch. Their expertise, time, and effort have been invaluable to us. We’d like to thank the Pragmatic Programmers for giving us a great opportunity to spread the word of Rails and our editor, Susannah, for keeping us on track. Thanks to everyone building open source software th at we use and love, from Rails to PHP. They truly make our working lives so much easier to enjoy. www.it-ebooks.info [...]... testing easy The Rails stack builds on this to provide clear, built-in support for testing all parts of a web application from the first line of code Who Should Read This Book This book is meant for PHP developers who are interested in adding Rails to their toolsets There are a lot of books on Rails now, but PHP developers have a unique way of thinking about problems that are built around the PHP mind-set... toolbox Although we’ll use PHP to drive our learning of Rails, Rails can help us learn about PHP as well As developers, we’re always on a quest to find faster and more efficient ways to do our job We hope that Rails inspires you to do just that 16 www.it-ebooks.info Part I From PHP to Rails www.it-ebooks.info Chapter 1 Getting Started with Rails In this chapter we’ll begin our Rails journey by focusing... PHP to Rails • Part II, “Building a Rails Application” • Part III, PHP to Ruby at a Glance” The first part—“From PHP to Rails —introduces the Model/View/Controller pattern with the conversion of a simple PHP application to Rails This part then presents an introduction of Ruby to lay the foundation for building a larger Rails application The second part—“Building a Rails Application”—guides you through... between PHP and Ruby The PHP examples will work on PHP 5.1 or newer For those of you who are still working primarily with PHP 4, you may occasionally see PHP features mentioned that you haven’t used yet, such as exceptions You’ll want to consult the PHP manual on these as you go The Ruby and Rails examples will all run on recent Ruby versions but are especially geared toward Ruby 1.8.5 and newer Before... to the gem? [Yn] Y Removing rails Let’s now install Rails work> gem install rails Successfully installed rails- 2.0.2 Congratulations, you should now have Ruby and Rails up and running on your system To make sure that we’re working with the correct version of Rails, we can run the rails command with the -v option work> rails -v Rails 2.0.2 Although Rails works with a variety of databases, the examples... differs from PHP 1.1 Rails as an Extension of Ruby David Heinemeier Hansson extracted Rails from an application he wrote for his company, 37signals He released it as open source in 2004, and there is now a group of developers on the core team actively applying features and patches to Rails David’s original framework actually began in PHP, but he later found Ruby to be a much better fit for what he needed... making the transition from PHP to Ruby and Rails is more than learning a new language syntax Before we start coding, let’s go over some of the Rails conventions that will inform the decisions we make as we write our applications 1.3 Opinionated Software The Ruby and Rails culture is quite different from that in PHP, and this is reflected in both the code and the community Rails is considered opinionated... introduction of PHP 5, we’ve witnessed an evolution in PHP s capabilities Perhaps even more than changes in PHP itself, we’ve seen a transformation in the way programmers use it Object-oriented programming has become more commonplace As a result, professional software engineering practices such as unit testing have become more practical and accessible to PHP developers We were early adopters of PHP 5 When Rails. .. more and to get you accustomed to how code is organized in Rails For our first Rails application, we need to create a simple form to collect email addresses for a company newsletter Before saving any emails, we validate that the address is unique and formatted correctly We then notify users of any errors that happen during the operation Each Rails application is stored within its own directory To create... aims to guide your learning in Rails based on your existing knowledge of programming in PHP An understanding of object-oriented programming in PHP will help but is not entirely necessary This should be something you start to pick up naturally while programming in Ruby Through this book, you will likely learn valuable lessons from Rails that will inform future PHP development Rails assembles a collection . saying about Rails for PHP Developers This is a thorough and approachable introduction to Ruby and Rails for PHP programmers from fellow developers who are well-versed in both Ruby and PHP. Paul. Framework for PHP As a PHP developer, I found the book focused well on the transition from coding PHP to coding Ruby (and Rails) and that it gave great examples of translating common PHP idioms. articles. PHP and Rails: A Personal View Since the introduction of PHP 5, we’ve witnessed an evolution in PHP s capabilities. Perhaps even more than changes in PHP itself, we’ve seen a transformation

Ngày đăng: 29/03/2014, 15:20