9781786469656 Preview.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Laravel 5.x Cookbook A recipe-based book to help you effciently create amazing PHP-based applications with Laravel 5.x Alfred Nutile BIRMINGHAM - MUMBAI Laravel 5.x Cookbook Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2016 Production reference: 1290816 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-208-4 www.packtpub.com Credits Author Project Coordinator Alfred Nutile Sheejal Shah Reviewer Proofreader Andrew Cavanagh Safs Editing Commissioning Editor Indexer Amarabha Banerjee Tejal Daruwale Soni Acquisition Editor Graphics Reshma Raman Abhinash Sahu Content Development Editor Production Coordinator Parshva Sheth Aparna Bhagat Technical Editor Cover Work Gebin George Aparna Bhagat Prajakta Mhatre Copy Editor Safs Editing About the Author Alfred Nutile is an Enterprise Architect and Laravel lead based in Western Massachusetts. He's been working in the industry since the mid 90's. He started in PHP and MySQL back around that time and has worked with Ruby on Rails, Drupal, and Angular along the way. He introduced Laravel into an enterprise web stack, where he is currently contracted at, as Laravel proved itself as an amazing framework to build API's and Angular heavy application. You can read more about him and checkout his blog at http://www.alfrednutile. info/ or follow him on Twitter at https://twitter.com/alnutile Make sure to checkout the discount LaraCasts is offering those who buy the book! Get a coupon for 50% on your frst bill. Make it a yearly subscription and save $43! Coupon Code: LaracastsLovesPackt https://laracasts.com/signup?plan=yearly&coupon=LaracastsLovesPackt. First let me say thanks to Taylor Otwell, I moved away from PHP to Rails and really was missing PHP only to be pointed to Laravel and have not looked back since. The framework makes my day to day work a pleasure and sometimes I feel guilty how easy it has made getting work done. And Jeffrey Way for offering an amazing service that helped me to grow in understanding practical programming skills, keeping things simple, best practices and Laravel. Of course I would like to thank Andrew Cavanagh for taking time to tediously edit this book. We have worked in the industry together for a number of years and currently both working on Laravel daily. And then there is my family who keeps me from getting too out of balanced since it is so easy to lose myself in the joy of building things on the computer! About the Reviewer Andrew Cavanagh is an experienced PHP developer whose background includes Drupal, Laravel, AngularJS, managing *nix servers (Apache and Nginx), project/budget/team management, and general haberdashy. He's currently focused on building enterprise web applications and tools. Andrew also enjoy cooking, amateur mycology, brewing, and long walks in imaginary places. www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print, and bookmark content f On demand and accessible via a web browser Table of Contents Preface v Chapter 1: Setting Up and Installing Laravel 1 Introduction 1 Setting up Homestead 1 Setting composer and PHP on your local machine for faster Workfows 4 Setting up your frst application in Homestead 6 Using .env for your local build 9 Using sequel pro and connecting to local and remote databases 12 Setting up Gulp and Elixir 14 Chapter 2: Using Composer Packages 17 Introduction 17 Working with Composer install command and avoiding Composer update 17 Making a provider 21 Using the Facade pattern 27 Using private packages 29 Chapter 3: Routing 33 Introduction 33 Building an API / JSON based route for searching 33 Testing your route in PHPUnit 37 Building a view based route 40 Testing your view based route in PHPUnit 42 Creating named routes 45 Chapter 4: Building Views and Adding Style 49 Introduction 49 Organizing your Blade fles 49 Installing a WrapBootStrap theme 55 Making your authentication pages 61 i Table of Contents Implementing an error message template 65 Building your main search page 70 Adding a static page 76 Chapter 5: Working with Data 79 Introduction 79 Setting up users and running migrations 79 Altering a migration 81 Using factories for migrations and tests 84 Using a generator to scaffold your user wishlist area 90 Seeding so you can see how your app looks 95 Adding a fle upload to user profle 99 Validating the fle upload 108 Saving fles to S3 112 Chapter 6: Adding Angular to Your App 123 Introduction 123 Adding Angular search to our search page 130 Handling Angular and Ajax requests 138 Paginating our Angular results 149 Testing an angular page with Behat 157 Creating a relationship with favorites 161 Building a favorites Ajax widget in Angular 164 Validating incoming input 173 Using the CORS protection 176 Using Elixir and Gulp to set up Angular 183 Chapter 7: Authentication, Security, and Subscriptions 191 Introduction 191 Using policies and guard to protect user pages 192 Adding feature fags to hide features from users 196 Implementing Socialite to allow users to login with Facebook 201 Adding custom middleware to protect user admin area 206 Using Laravel to set up a subscription site 211 Creating an interface for the user to manage subscriptions 222 Creating an admin interface for subscriptions 225 ii Table of Contents Chapter 8: Testing and Debugging Your Application 233 Introduction 233 Generating tests 234 Using tests to think through your code TDT (Test Driven Thinking) 236 Getting your code onto GitHub 244 Using VCR for API testing 247 Using Travis to run tests with every push 252 Launching Gulp watch into your workfow 259 Using mockery to test your controllers 261 Troubleshooting your application 268 Chapter 9: Adding Advanced Features to Your App 271 Introduction 271 Building an Artisan command 272 Creating scheduler to notify users of new comics 277 Setting up e-mail notices 280 Adding clean URLS for the users profle page 287 Using pusher for live notifcations 293 Adding a blog area to update users on new features 303 Chapter 10: Deploying Your App 323 Introduction 323 Setting up Forge, AWS, and CodeDeploy 324 Setting up Travis to auto deploy when all is passing 353 Working with your .env fle 355 Testing your app on Production with Behat 359 Making a composer package out of our client 366 Index 381 iii Preface Laravel is a prominent member of a new generation of web frameworks. It is one of the most popular PHP frameworks and is also free and an open source. Laravel 5 is a substantial upgrade with a lot of new and more effcient workfows, at the same time retaining the features that made Laravel wildly successful. It comes with plenty of architectural as well as design-based changes. The book is a blend of numerous recipes that will give you all the necessary tips you need to build an application. It starts with basic installation and confguration tasks and will get you up-and-running in no time. You will learn to create and customize your PHP app and tweak and re-design your existing apps for better performance. You will learn to implement practical recipes to utilize Laravel's modular structure, the latest method injection, route caching, and interfacing techniques to create responsive modern-day PHP apps that stand on their own against other apps. Effcient testing and deploying techniques will make you more confdent with your Laravel skills as you move ahead with this book. Towards the end of the book, you will understand a number of add-ons and new features essential to fnalize your application to make it ready for subscriptions. You will be empowered to get your application out to the world. What this book covers Chapter 1, Setting Up and Installing Laravel, helps you setup Laravel on your local machine. Chapter 2, Using Composer Packages, this will cover some key concepts to getting comfortable with this amazing tool. Chapter 3, Routing, the heart of any web framework, this will dig into some use cases of Laravel routing.