ebook img

Laravel: Up & Running: A Framework for Building Modern PHP Apps PDF

544 Pages·2019·7.82 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Laravel: Up & Running: A Framework for Building Modern PHP Apps

S e E c dio tin d o n Laravel Up & Running A Framework for Building Modern PHP Apps Matt Stauffer SECOND EDITION Laravel: Up & Running A Framework for Building Modern PHP Apps Matt Stauffer BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Laravel: Up & Running by Matt Stauffer Copyright © 2019 Matt Stauffer. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Alicia Young Indexer: WordCo Indexing Services, Inc. Production Editor: Christopher Faucher Interior Designer: David Futato Copyeditor: Rachel Head Cover Designer: Karen Montgomery Proofreader: Amanda Kersey Illustrator: Rebecca Demarest December 2016: First Edition April 2019: Second Edition Revision History for the Second Edition 2019-04-01: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492041214 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Laravel: Up & Running, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author, and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-04121-4 [LSI] This book is dedicated to my family. Mia, my little princess and bundle of joy and energy. Malachi, my little prince and adventurer and empath. Tereva, my inspiration, encourager, upgrader, pusher, rib. Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 1. Why Laravel?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Why Use a Framework? 1 “I’ll Just Build It Myself” 2 Consistency and Flexibility 2 A Short History of Web and PHP Frameworks 2 Ruby on Rails 2 The Influx of PHP Frameworks 3 The Good and the Bad of CodeIgniter 3 Laravel 1, 2, and 3 3 Laravel 4 4 Laravel 5 4 What’s So Special About Laravel? 4 The Philosophy of Laravel 5 How Laravel Achieves Developer Happiness 5 The Laravel Community 6 How It Works 7 Why Laravel? 9 2. Setting Up a Laravel Development Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 System Requirements 11 Composer 12 Local Development Environments 12 Laravel Valet 12 Laravel Homestead 13 Creating a New Laravel Project 13 Installing Laravel with the Laravel Installer Tool 14 v Installing Laravel with Composer’s create-project Feature 14 Lambo: Super-Powered “Laravel New” 14 Laravel’s Directory Structure 15 The Folders 16 The Loose Files 17 Configuration 18 The .env File 19 Up and Running 21 Testing 21 TL;DR 22 3. Routing and Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 A Quick Introduction to MVC, the HTTP Verbs, and REST 23 What Is MVC? 23 The HTTP Verbs 24 What Is REST? 25 Route Definitions 26 Route Verbs 28 Route Handling 28 Route Parameters 29 Route Names 31 Route Groups 33 Middleware 34 Path Prefixes 36 Fallback Routes 36 Subdomain Routing 37 Namespace Prefixes 37 Name Prefixes 38 Signed Routes 38 Signing a Route 39 Modifying Routes to Allow Signed Links 40 Views 40 Returning Simple Routes Directly with Route::view() 41 Using View Composers to Share Variables with Every View 42 Controllers 42 Getting User Input 45 Injecting Dependencies into Controllers 46 Resource Controllers 47 API Resource Controllers 49 Single Action Controllers 49 Route Model Binding 50 Implicit Route Model Binding 50 vi | Table of Contents Custom Route Model Binding 51 Route Caching 52 Form Method Spoofing 52 HTTP Verbs in Laravel 52 HTTP Method Spoofing in HTML Forms 53 CSRF Protection 53 Redirects 55 redirect()->to() 56 redirect()->route() 56 redirect()->back() 57 Other Redirect Methods 57 redirect()->with() 57 Aborting the Request 59 Custom Responses 60 response()->make() 60 response()->json() and ->jsonp() 60 response()->download(), ->streamDownload(), and ->file() 60 Testing 61 TL;DR 62 4. Blade Templating. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Echoing Data 64 Control Structures 65 Conditionals 65 Loops 65 Template Inheritance 68 Defining Sections with @section/@show and @yield 68 Including View Partials 70 Using Stacks 72 Using Components and Slots 73 View Composers and Service Injection 75 Binding Data to Views Using View Composers 76 Blade Service Injection 79 Custom Blade Directives 80 Parameters in Custom Blade Directives 81 Example: Using Custom Blade Directives for a Multitenant App 82 Easier Custom Directives for “if” Statements 83 Testing 83 TL;DR 84 5. Databases and Eloquent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Configuration 87 Table of Contents | vii Database Connections 88 Other Database Configuration Options 89 Defining Migrations 90 Running Migrations 97 Seeding 98 Creating a Seeder 98 Model Factories 99 Query Builder 105 Basic Usage of the DB Facade 105 Raw SQL 106 Chaining with the Query Builder 107 Transactions 116 Introduction to Eloquent 117 Creating and Defining Eloquent Models 119 Retrieving Data with Eloquent 120 Inserts and Updates with Eloquent 122 Deleting with Eloquent 126 Scopes 128 Customizing Field Interactions with Accessors, Mutators, and Attribute Casting 131 Eloquent Collections 135 Eloquent Serialization 137 Eloquent Relationships 139 Child Records Updating Parent Record Timestamps 152 Eloquent Events 154 Testing 155 TL;DR 157 6. Frontend Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Laravel Mix 159 Mix Folder Structure 161 Running Mix 161 What Does Mix Provide? 162 Frontend Presets and Auth Scaffolding 169 Frontend Presets 169 Auth Scaffolding 170 Pagination 170 Paginating Database Results 170 Manually Creating Paginators 171 Message Bags 172 Named Error Bags 174 String Helpers, Pluralization, and Localization 174 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.