Easy Laravel 5 a Hands on Introduction Using a Real-World Project
Total Page:16
File Type:pdf, Size:1020Kb
Easy Laravel 5 A Hands On Introduction Using a Real-World Project W. Jason Gilmore This book is for sale at http://leanpub.com/easylaravel This version was published on 2015-08-11 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2014 - 2015 W. Jason Gilmore Also By W. Jason Gilmore Easy Active Record for Rails Developers Easy E-Commerce Using Laravel and Stripe Easy React Dedicated to The Champ, The Princess, and Little Winnie. Love, Daddy Contents Introduction ............................................ 1 What’s New in Laravel 5? ................................... 2 About this Book ......................................... 2 Introducing the TODOParrot Project .............................. 5 About the Author ........................................ 5 Errata and Suggestions ..................................... 6 Chapter 1. Introducing Laravel ................................. 7 Installing Laravel ........................................ 7 Creating the TODOParrot Application ............................. 14 Configuring Your Laravel Application ............................. 19 Useful Development and Debugging Tools .......................... 22 Testing Your Laravel Application with PHPUnit ....................... 30 Conclusion ............................................ 33 Chapter 2. Managing Your Project Controllers, Layout, Views, and Other Assets ..... 34 Creating Your First View .................................... 34 Creating Your First Controller ................................. 36 Managing Your Application Routes .............................. 37 Introducing the Blade Template Engine ............................ 43 Integrating Images, CSS and JavaScript ............................ 52 Introducing Elixir ........................................ 56 Testing Your Views ....................................... 61 Conclusion ............................................ 64 Chapter 3. Introducing Laravel Models ............................. 65 Configuring Your Project Database .............................. 65 Introducing the Eloquent ORM ................................. 68 Creating Your First Model ................................... 68 Introducing Migrations ..................................... 69 Defining Accessors, Mutators, and Methods .......................... 77 Validating Your Models ..................................... 80 Creating a RESTful Controller ................................. 83 Seeding the Database ...................................... 89 CONTENTS Finding Data ........................................... 93 Inserting New Records .....................................104 Updating Existing Records ...................................106 Deleting Records ........................................106 Introducing Query Builder ...................................108 Creating Sluggable URLs ....................................110 Testing Your Models ......................................112 Summary ............................................117 Chapter 4. Model Relations, Scopes, and Other Advanced Features ............. 118 Introducing Relations ......................................118 Introducing One-to-One Relations ...............................119 Introducing the Belongs To Relation ..............................123 Introducing One-to-Many Relations ..............................124 Introducing Many-to-Many Relations .............................128 Introducing Has Many Through Relations . 137 Introducing Polymorphic Relations ..............................138 Eager Loading ..........................................141 Introducing Scopes .......................................142 Summary ............................................144 Chapter 5. Integrating Web Forms ............................... 145 Web Form Fundamentals ....................................145 Creating a User Feedback Form ................................148 Creating New TODO Lists ...................................159 Updating a TODO List .....................................164 Deleting TODO Lists ......................................167 Associating Tasks with Categories ...............................168 Uploading Files .........................................172 Summary ............................................175 Chapter 6. Introducing Middleware ............................... 176 Introducing Laravel’s Default Middleware . 176 Creating Your Own Middleware Solution . 180 Using Middleware Parameters .................................182 Summary ............................................183 Chapter 7. Authenticating and Managing Your Users ..................... 184 Configuration Laravel Authentication .............................184 Registering Users ........................................185 Retrieving the Authenticated User ...............................195 Restricting Access to Authenticated Users . 196 Restricting Forms to Authenticated Users . 196 Creating Route Aliases .....................................197 CONTENTS Adding New Properties to the User Model . 197 Chapter 8. Deploying, Optimizing and Maintaining Your Application ........... 198 Introducing the Laravel 5 Command Scheduler . 198 Optimizing Your Application ..................................203 Deploying Your Application ..................................206 Placing Your Application in Maintenance Mode . 213 Summary ............................................214 Chapter 9. Creating a Restricted Administration Console .................. 215 Identifying Administrators ...................................215 Creating the Administration Controllers ............................216 Restricting Access to the Administration Console . 218 Summary ............................................219 Chapter 10. Introducing Lumen ................................. 220 Creating Your First Lumen Application ............................221 Creating a Status API ......................................223 Integrating the Lumen Application Into TODOParrot.com . 227 Summary ............................................228 Chapter 11. Introducing Events ................................. 229 Creating an Event ........................................229 Broadcasting Events .......................................235 Summary ............................................239 Appendix A. Deploying Your Laravel Application to DreamHost .............. 240 Deploying Your Project to DreamHost .............................241 Summary ............................................256 Introduction I’ve spent the vast majority of the past 15 years immersed in the PHP language. During this time I’ve written seven PHP-related books, including a bestseller that has been in print for more than ten years. Along the way I’ve worked on dozens of PHP-driven applications for clients ranging from unknown startups to globally-recognized companies, penned hundreds of articles about PHP and web development for some of the world’s most popular print and online publications, and instructed hundreds of developers in the United States and Europe. So you might be surprised to learn that a few years ago I became rather disenchanted with PHP. It felt like there were more exciting developments taking place within other programming communities, and wanting to be part of that buzz, I wandered off. In recent years, I spent the majority of my time working on a variety of projects including among others several ambitious Ruby on Rails applications and even a pretty amazing Linux-powered robotic device. Of course, even during this time in the wilderness I kept tabs on the PHP community, watching with great interest as numerous talented developers worked tirelessly to inject that missing enthusiasm back into the language. Nils Adermann and Jordi Boggiano released the Composer¹ dependency manager. The Framework Interoperability Group² was formed. And in 2012 the incredibly talented Taylor Otwell³ created the Laravel framework⁴ which out of nowhere became the most popular PHP project on GitHub, quickly surpassing projects and frameworks that had been actively developed for years. At some point I spent some time with Laravel and after a scant 30 minutes knew it was the real deal. Despite being the latest in a string of high profile PHP frameworks, Laravel is incredibly polished, offering a shallow learning curve, convenient PHPUnit integration, a great object- relational mapping solution called Eloquent, and a wide variety of other great features. The reasoning behind this pragmatic approach is laid bare in the project documentation⁵, in which the Laravel development team describes their project goals: Laravel aims to make the development process a pleasing one for the developer without sacrificing application functionality. Happy developers make the best code. To this end, we’ve attempted to combine the very best of what we have seen in other web frameworks, including frameworks implemented in other languages, such as Ruby on Rails, ASP.NET MVC, and Sinatra. Now that’s something to get excited about! In the pages to follow I promise to add you to the ranks of fervent Laravel users by providing a wide-ranging and practical introduction to its many features. ¹https://getcomposer.org/ ²http://www.php-fig.org/ ³http://taylorotwell.com/ ⁴http://laravel.com/ ⁵http://laravel.com/docs/master