Model View Controller (MVC) is becoming the definitive architecture of website development frameworks due to the stability, extensibility and predictability it lends to development. It is not just the primary separation of database, business logic and interface components, but includes a wide range of considerations for building high-performing, scalable and secure applications. Deciding which MVC framework best suits the project you are about to begin is one of the biggest challenges you'll face as a developer. If you are part of a team, this decision has probably already been made for you; but in any event, you'll need to know how (and why) the framework authors made it work the way it does. Pro PHP MVC looks at the building blocks that make any good MVC framework, and how they apply to PHP. It exposes all considerations that many developers take for granted when using a popular framework, and teaches you how to make the framework your own. Over the course of reading this book, you will learn the theoretical implications of the choices you would make when writing your own MVC framework, and how to put the pieces together in a cohesive package. We take a look at the highly modular Zend Framework—how to use its collection of loosely coupled classes to build a unified system. We also look at CakePHP, learning from its automated build system (Bakery) and highly intuitive approach to rapid development. This book will lay bare all the secret parts of MVC for you. Apress Pro PHP MVC 462 Contents at a Glance 3 Contents 465 About the Author 477 About the Technical Reviewer 478 Acknowledgments 479 Introduction 5 Chapter 1: Introduction to MVC 6 What Is MVC? 6 Benefits of MVC 7 Popular MVC Frameworks 8 CodeIgniter 8 Zend Framework 9 CakePHP 9 Design Patterns 9 Singleton 9 Registry 10 Factory 11 Observer 11 Creating Our Own Framework 12 Goals 12 Chapter 2: Foundation 13 Goals 13 Autoloading 13 Namespaces 14 Lazy Loading 15 Exceptions 16 Type Methods 17 Metadata 19 Questions 28 Answers 28 Exercises 29 Chapter 3: Base Class 30 Goals 30 Getters and Setters 30 Magic Methods 33 Adding Introspection 34 Transparent Getters/Setters 37 Questions 41 Answers 41 Exercises 42 Chapter 4: Configuration 43 Goals 43 Associative Arrays 43 INI Files 44 Questions 51 Answers 51 Exercises 51 Chapter 5: Caching 52 Goals 52 Performance Bottlenecks 52 The Code 53 Questions 60 Answers 60 Exercises 61 Chapter 6: Registry 62 Goals 62 Singleton 62 Registry 64 Questions 66 Answers 66 Exercises 66 Chapter 7: Routing 67 Goals 67 Defining Routes 67 Route Classes 68 Router Class 71 Questions 82 Answers 82 Exercises 82 Chapter 8: Templates 83 Goals 83 Idea 83 Alternatives 84 Implementation 84 Benefits 102 Questions 112 Answers 112 Exercises 112 Chapter 9: Databases 113 Goals 113 Implementation 113 Connectors 116 Queries 120 Questions 142 Answers 142 Exercises 142 Chapter 10: Models 143 Goals 143 Idea 143 Implementation 144 Building the SQL 146 Modifying Records 156 No Relation of Mine! 170 Questions 170 Answers 170 Exercises 171 Chapter 11: Testing 172 Goals 172 Unit Testing 172 Test Class 172 Cache 174 Coverage 174 Tests 175 Configuration 178 Coverage 178 Tests 178 Database 179 Coverage 179 Tests 180 Model 188 Coverage 188 Tests 188 Template 191 Coverage 191 Tests 191 Holy Code, Batman! 194 Questions 194 Answers 194 Exercises 194 Chapter 12: Structure 195 Goals 195 Database 195 Folders 196 Questions 197 Answers 197 Chapter 13: Bootstrapping 198 Goals 198 When a File Is Not a File... 198 URL Rewriting 199 Index.php 200 Configuration 201 Database 202 Cache 203 Controller 204 Views 205 Rendering 207 Questions 213 Answers 213 Exercises 214 Chapter 14: Registration and Login 215 Goals 215 Shared Libraries 215 User Model 216 Registration 219 Sessions 224 Login 227 Questions 235 Answers 235 Exercises 235 Chapter 15: Search 236 Goals 236 Extended Implementation 237 URL Requests 239 Search 248 Questions 254 Answers 255 Exercises 255 Chapter 16: Settings 256 Goals 256 Validation 256 Validate As Required 262 Settings 262 Questions 267 Answers 267 Exercises 267 Chapter 17: Sharing 268 Goals 268 Error Pages 268 Friends 272 Sharing 279 Questions 283 Answers 283 Exercises 283 Chapter 18: Photos 284 Goals 284 How to Upload Files 284 User Photos 285 Showing Off, a Little 289 Questions 290 Answers 291 Exercises 291 Chapter 19: Extending 292 Goals 292 Foxy 292 Custom CSS Fonts 292 Building the Proxy 293 Using the Proxy 297 Imagine 300 Observer 302 Synchronicity 302 Code 304 Events 306 Plugins 309 Questions 313 Answers 313 Exercises 314 Chapter 20: Administration 315 Goals 315 What Is a CMS ? 315 Administrators 315 Login 316 Users 322 Photos 328 Questions 330 Answers 330 Exercises 331 Chapter 21: Testing 332 Goals 332 Questions 335 Answers 335 Exercises 336 Chapter 22: CodeIgniter: Bootstrapping 337 Goals 337 Why CodeIgniter? 337 Why Not CodeIgniter? 338 URL Rewriting 338 Routes 338 Questions 339 Answers 340 Exercises 340 Chapter 23 : CodeIgniter: MVC 341 Goals 341 Differences 341 Models 341 Controllers 346 Questions 357 Answers 357 Exercises 357 Chapter 24: CodeIgniter: Extending 358 Goals 358 File Uploads 358 Third-Party Libraries 365 Extending the Core 367 Questions 369 Answers 369 Exercises 369 Chapter 25: CodeIgniter: Testing 370 Goals 370 Tools 370 The Alternative 371 Questions 371 Answers 371 Exercises 372 Chapter 26: Zend Framework: Bootstrapping 373 Goals 373 Why Zend Framework? 373 Why Not Zend Framework? 373 Getting Set Up 374 Routes 374 Questions 375 Answers 376 Exercises 376 Chapter 27: Zend Framework: MVC 377 Goals 377 The Differences 377 Models 377 Controllers 384 Questions 393 Answers 394 Exercises 394 Chapter 28: Zend Framework: Extending 395 Goals 395 File Uploads 395 Third-Party Libraries 402 Questions 404 Answers 404 Exercises 404 Chapter 29: Zend Framework: Testing 405 Goals 405 Installing PEAR 405 Windows 405 Unix/Linux/BSD 406 Mac OS X 406 Installing PHPUnit 406 Running the Tests 406 Adding Tests 406 Questions 408 Answers 408 Chapter 30: CakePHP: Bootstrapping 409 Goals 409 Why CakePHP? 409 Why Not CakePHP? 409 Getting Set Up 410 Routes 410 Questions 411 Answers 411 Exercises 412 Chapter 31: CakePHP: MVC 413 Goals 413 What’s in a Model? 413 Controllers 415 Finishing Up 416 Questions 421 Answers 421 Exercises 422 Chapter 32: CakePHP: Extending 423 Goals 423 File Uploads 423 Third-Party Libraries 427 Plugins 427 Vendor Directory 427 Questions 429 Answers 429 Exercises 430 Chapter 33: CakePHP: Testing 431 Goals 431 Testing 431 Questions 433 Answers 433 Exercises 434 APPENDIX A: Setting Up a Web Server 435 Goals 435 Windows 435 Step 1 435 Naughty Skype ! 437 Configuring Apache/PHP 438 Step 2 439 What Is MSVCP71.dll? 441 Step 3 441 Linux 443 Step 1 443 Step 2 445 Step 3 446 Step 4 447 MAC OS X 448 Step 1 448 Step 2 449 What If wget Is Missing? 450 Step 3 452 You Passed, with Flying Colors! 453 Index 455 www.it-ebooks.info IT eBooks Annotation Model View Controller (MVC) is becoming the definitive architecture of website development frameworks due to the stability, extensibility and predictability it lends to development. It is not just the primary separation of database, business logic and interface components, but includes a wide range of considerations for building high-performing, scalable and secure applications. Deciding which MVC framework best suits the project you are about to begin is one of the biggest challenges you'll face as a developer. If you are part of a team, this decision has probably already been made for you; but in any event, you'll need to know how (and why) the framework authors made it work the way it does. Pro PHP MVC looks at the building blocks that make any good MVC framework, and how they apply to PHP. It exposes all considerations that many developers take for granted when using a popular framework, and teaches you how to make the framework your own. Over the course of reading this book, you will learn the theoretical implications of the choices you would make when writing your own MVC framework, and how to put the pieces together in a cohesive package. We take a look at the highly modular Zend Frameworkhow to use its collection oflooselycoupled classes to build a unified system. We also look at CakePHP, learning from its automated build system (Bakery) and highly intuitive approach to rapid development. This book will lay bare all the secret parts of MVC for you. What youll learnThe best way to structure your application filesHow to get from a URL to the right controller/actionCleanly interact with data objectsEfficient use of server resources in content deliveryUseful shared class libraries to speed up developmentUnit testing to ensure maximum stabilityWho this book is forThis book is for beginners and advanced developers alike, requiring a basic understanding of PHP5 language features and SQL. If you want to improve your PHP skills, or share the wisdom of popular PHP MVC frameworks, this book will be invaluable to you. Table of ContentsIntroductionFoundationBaseConfigurationCachingRegistryRoutingTemplatesDatabaseModelsTestingStructureBootstrappingRegistration + LoginSettingsSearchSharingPhotosExtendingAdministrationTestingCodeIgniter: BootstrappingCodeIgniter: MVCCodeIgniter: ExtendingCodeIgniter: TestingZend Framework: BootstrappingZend Framework: MVCZend Framework: ExtendingZend Framework: TestingCakePHP: BootstrappingCakePHP: MVCCakePHP: ExtendingCakePHP: TestingAppendix A: Setting Up a Web Server Model View Controller (MVC) is becoming the definitive architecture of website development frameworks due to the stability, extensibility and predictability it lends to development. It is not just the primary separation of database, business logic and interface components, but includes a wide range of considerations for building high-performing, scalable and secure applications. Deciding which MVC framework best suits the project you are about to begin is one of the biggest challenges you'll face as a developer. If you are part of a team, this decision has probably already been made for you; but in any event, you'll need to know how (and why) the framework authors made it work the way it does. Pro PHP MVC looks at the building blocks that make any good MVC framework, and how they apply to PHP. It exposes all considerations that many developers take for granted when using a popular framework, and teaches you how to make the framework your own. Over the course of reading this book, you will learn the theoretical implications of the choices you would make when writing your own MVC framework, and how to put the pieces together in a cohesive package. We take a look at the highly modular Zend Framework how to use its collection of loosely coupled classes to build a unified system. We also look at CakePHP, learning from its automated build system (Bakery) and highly intuitive approach to rapid development. This book will lay bare all the secret parts of MVC for you