Laravel: Up & Running, 2nd Edition

Book Description

What sets Laravel apart from other PHP web frameworks? Speed and simplicity, for starters. This rapid application development framework and its ecosystem of tools let you quickly build new sites and applications with clean, readable code. Fully updated to cover Laravel 5.8, the second edition of this practical guide provides the definitive introduction to one of today’s mostpopular web frameworks.

Matt Stauffer, a leading teacher and developer in the Laravel community, delivers a high-level overview and concrete examples to help experienced PHP web developers get started with this framework right away. This updated edition also covers Laravel Dusk and Horizon and provides information about community resources and other noncore Laravel packages.

Dive into features, including:

  • Blade, Laravel’s powerful custom templating tool
  • Tools for gathering, validating, normalizing, and filtering user-provideddata
  • The Eloquent ORM for working with application databases
  • The role of the Illuminate request object in the application lifecycle
  • PHPUnit, Mockery, and Dusk for testing your PHP code
  • Tools for writing JSON and RESTful APIs
  • Interfaces for filesystem access, sessions, cookies, caches, and search
  • Tools for implementing queues, jobs, events, and WebSocket event publishing

Table of Contents

  1. Preface
    1. What This Book Is About
    2. Who This Book Is For
    3. How This Book Is Structured
    4. About the Second Edition
    5. Conventions Used in This Book
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments for the First Edition
    9. Acknowledgments for the Second Edition
  2. 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
      1. Ruby on Rails
      2. The Influx of PHP Frameworks
      3. The Good and the Bad of CodeIgniter
      4. Laravel 1, 2, and 3
      5. Laravel 4
      6. Laravel 5
      7. Laravel 6
    3. What’s So Special About Laravel?
      1. The Philosophy of Laravel
      2. How Laravel Achieves Developer Happiness
      3. The Laravel Community
    4. How It Works
    5. Why Laravel?
  3. 2. Setting Up a Laravel Development Environment
    1. System Requirements
    2. Composer
    3. Local Development Environments
      1. Laravel Valet
      2. Laravel Homestead
    4. Creating a New Laravel Project
      1. Installing Laravel with the Laravel Installer Tool
      2. Installing Laravel with Composer’s create-project Feature
      3. Lambo: Super-Powered “Laravel New”
    5. Laravel’s Directory Structure
      1. The Folders
      2. The Loose Files
    6. Configuration
      1. The .env File
    7. Up and Running
    8. Testing
    9. TL;DR
  4. 3. Routing and Controllers
    1. A Quick Introduction to MVC, the HTTP Verbs, and REST
      1. What Is MVC?
      2. The HTTP Verbs
      3. What Is REST?
    2. Route Definitions
      1. Route Verbs
      2. Route Handling
      3. Route Parameters
      4. Route Names
    3. Route Groups
      1. Middleware
      2. Path Prefixes
      3. Fallback Routes
      4. Subdomain Routing
      5. Namespace Prefixes
      6. Name Prefixes
    4. Signed Routes
      1. Signing a Route
      2. Modifying Routes to Allow Signed Links
    5. Views
      1. Returning Simple Routes Directly with Route::view()
      2. Using View Composers to Share Variables with Every View
    6. Controllers
      1. Getting User Input
      2. Injecting Dependencies into Controllers
      3. Resource Controllers
      4. API Resource Controllers
      5. Single Action Controllers
    7. Route Model Binding
      1. Implicit Route Model Binding
      2. Custom Route Model Binding
    8. Route Caching
    9. Form Method Spoofing
      1. HTTP Verbs in Laravel
      2. HTTP Method Spoofing in HTML Forms
    10. CSRF Protection
    11. Redirects
      1. redirect()->to()
      2. redirect()->route()
      3. redirect()->back()
      4. Other Redirect Methods
      5. redirect()->with()
    12. Aborting the Request
    13. Custom Responses
      1. response()->make()
      2. response()->json() and ->jsonp()
      3. response()->download(), ->streamDownload(), and ->file()
    14. Testing
    15. TL;DR
  5. 4. Blade Templating
    1. Echoing Data
    2. Control Structures
      1. Conditionals
      2. Loops
    3. Template Inheritance
      1. Defining Sections with @section/@show and @yield
      2. Including View Partials
      3. Using Stacks
      4. Using Components and Slots
    4. View Composers and Service Injection
      1. Binding Data to Views Using View Composers
      2. Blade Service Injection
    5. Custom Blade Directives
      1. Parameters in Custom Blade Directives
      2. Example: Using Custom Blade Directives for a Multitenant App
      3. Easier Custom Directives for “if” Statements
    6. Testing
    7. TL;DR
  6. 5. Databases and Eloquent
    1. Configuration
      1. Database Connections
      2. URL Configurations
      3. Other Database Configuration Options
    2. Migrations
      1. Defining Migrations
      2. Running Migrations
    3. Seeding
      1. Creating a Seeder
      2. Model Factories
    4. Query Builder
      1. Basic Usage of the DB Facade
      2. Raw SQL
      3. Chaining with the Query Builder
      4. Transactions
    5. Introduction to Eloquent
      1. Creating and Defining Eloquent Models
      2. Retrieving Data with Eloquent
      3. Inserts and Updates with Eloquent
      4. Deleting with Eloquent
      5. Scopes
      6. Customizing Field Interactions with Accessors, Mutators, and Attribute Casting
      7. Eloquent Collections
      8. Eloquent Serialization
      9. Eloquent Relationships
      10. Child Records Updating Parent Record Timestamps
    6. Eloquent Events
    7. Testing
    8. TL;DR
  7. 6. Frontend Components
    1. Laravel Mix
      1. Mix Folder Structure
      2. Running Mix
      3. What Does Mix Provide?
    2. Laravel UI: Frontend Presets and Auth Scaffolding
      1. Frontend Presets
      2. Auth Scaffolding
    3. Pagination
      1. Paginating Database Results
      2. Manually Creating Paginators
    4. Message Bags
      1. Named Error Bags
    5. String Helpers, Pluralization, and Localization
      1. The String Helpers and Pluralization
      2. Localization
    6. Testing
      1. Testing Message and Error Bags
      2. Translation and Localization
    7. TL;DR
  8. 7. Collecting and Handling User Data
    1. Injecting a Request Object
      1. $request->all()
      2. $request->except() and $request->only()
      3. $request->has()
      4. $request->input()
      5. $request->method() and ->isMethod()
      6. Array Input
      7. JSON Input (and $request->json())
    2. Route Data
      1. From Request
      2. From Route Parameters
    3. Uploaded Files
    4. Validation
      1. validate() on the Request Object
      2. Manual Validation
      3. Custom Rule Objects
      4. Displaying Validation Error Messages
    5. Form Requests
      1. Creating a Form Request
      2. Using a Form Request
    6. Eloquent Model Mass Assignment
    7. {{ Versus {!!
    8. Testing
    9. TL;DR
  9. 8. Artisan and Tinker
    1. An Introduction to Artisan
    2. Basic Artisan Commands
      1. Options
      2. The Grouped Commands
    3. Writing Custom Artisan Commands
      1. A Sample Command
      2. Arguments and Options
      3. Using Input
      4. Prompts
      5. Output
      6. Writing Closure-Based Commands
    4. Calling Artisan Commands in Normal Code
    5. Tinker
    6. Laravel Dump Server
    7. Testing
    8. TL;DR
  10. 9. User Authentication and Authorization
    1. The User Model and Migration
    2. Using the auth() Global Helper and the Auth Facade
    3. The Auth Controllers
      1. RegisterController
      2. LoginController
      3. ResetPasswordController
      4. ForgotPasswordController
      5. VerificationController
    4. Auth::routes()
    5. The Auth Scaffold
    6. “Remember Me”
    7. Password Confirmation
    8. Manually Authenticating Users
    9. Manually Logging Out a User
      1. Invalidating Sessions on Other Devices
    10. Auth Middleware
    11. Email Verification
    12. Blade Authentication Directives
    13. Guards
      1. Changing the Default Guard
      2. Using Other Guards Without Changing the Default
      3. Adding a New Guard
      4. Closure Request Guards
      5. Creating a Custom User Provider
      6. Custom User Providers for Nonrelational Databases
    14. Auth Events
    15. Authorization (ACL) and Roles
      1. Defining Authorization Rules
      2. The Gate Facade (and Injecting Gate)
      3. Resource Gates
      4. The Authorize Middleware
      5. Controller Authorization
      6. Checking on the User Instance
      7. Blade Checks
      8. Intercepting Checks
      9. Policies
    16. Testing
    17. TL;DR
  11. 10. Requests, Responses, and Middleware
    1. Laravel’s Request Lifecycle
      1. Bootstrapping the Application
      2. Service Providers
    2. The Request Object
      1. Getting a Request Object in Laravel
      2. Getting Basic Information About a Request
    3. The Response Object
      1. Using and Creating Response Objects in Controllers
      2. Specialized Response Types
    4. Laravel and Middleware
      1. An Introduction to Middleware
      2. Creating Custom Middleware
      3. Binding Middleware
      4. Passing Parameters to Middleware
    5. Trusted Proxies
    6. Testing
    7. TL;DR
  12. 11. The Container
    1. A Quick Introduction to Dependency Injection
    2. Dependency Injection and Laravel
    3. The app() Global Helper
    4. How the Container Is Wired
    5. Binding Classes to the Container
      1. Binding to a Closure
      2. Binding to Singletons, Aliases, and Instances
      3. Binding a Concrete Instance to an Interface
      4. Contextual Binding
    6. Constructor Injection in Laravel Framework Files
    7. Method Injection
    8. Facades and the Container
      1. How Facades Work
      2. Real-Time Facades
    9. Service Providers
    10. Testing
    11. TL;DR
  13. 12. Testing
    1. Testing Basics
    2. Naming Tests
    3. The Testing Environment
    4. The Testing Traits
      1. RefreshDatabase
      2. WithoutMiddleware
      3. DatabaseMigrations
      4. DatabaseTransactions
    5. Simple Unit Tests
    6. Application Testing: How It Works
      1. TestCase
    7. HTTP Tests
      1. Testing Basic Pages with $this->get() and Other HTTP Calls
      2. Testing JSON APIs with $this->getJson() and Other JSON HTTP Calls
      3. Assertions Against $response
      4. Authenticating Responses
      5. A Few Other Customizations to Your HTTP Tests
      6. Handling Exceptions in Application Tests
      7. Debugging Responses
    8. Database Tests
      1. Using Model Factories in Tests
      2. Seeding in Tests
    9. Testing Other Laravel Systems
      1. Event Fakes
      2. Bus and Queue Fakes
      3. Mail Fakes
      4. Notification Fakes
      5. Storage Fakes
    10. Mocking
      1. A Quick Introduction to Mocking
      2. A Quick Introduction to Mockery
      3. Faking Other Facades
    11. Testing Artisan Commands
      1. Asserting Against Artisan Command Syntax
    12. Browser Tests
      1. Choosing a Tool
      2. Testing with Dusk
    13. TL;DR
  14. 13. Writing APIs
    1. The Basics of REST-Like JSON APIs
    2. Controller Organization and JSON Returns
    3. Reading and Sending Headers
      1. Sending Response Headers in Laravel
      2. Reading Request Headers in Laravel
    4. Eloquent Pagination
    5. Sorting and Filtering
      1. Sorting Your API Results
      2. Filtering Your API Results
    6. Transforming Results
      1. Writing Your Own Transformer
      2. Nesting and Relationships with Custom Transformers
    7. API Resources
      1. Creating a Resource Class
      2. Resource Collections
      3. Nesting Relationships
      4. Using Pagination with API Resources
      5. Conditionally Applying Attributes
      6. More Customizations for API Resources
    8. API Authentication with Laravel Passport
      1. A Brief Introduction to OAuth 2.0
      2. Installing Passport
      3. Passport’s API
      4. Passport’s Available Grant Types
      5. Managing Clients and Tokens with the Passport API and Vue Components
      6. Passport Scopes
      7. Deploying Passport
    9. API Token Authentication
    10. Customizing 404 Responses
      1. Triggering the Fallback Route
    11. Testing
      1. Testing Passport
    12. TL;DR
  15. 14. Storage and Retrieval
    1. Local and Cloud File Managers
      1. Configuring File Access
      2. Using the Storage Facade
      3. Adding Additional Flysystem Providers
    2. Basic File Uploads and Manipulation
    3. Simple File Downloads
    4. Sessions
      1. Accessing the Session
      2. Methods Available on Session Instances
      3. Flash Session Storage
    5. Cache
      1. Accessing the Cache
      2. Methods Available on Cache Instances
    6. Cookies
      1. Cookies in Laravel
      2. Accessing the Cookie Tools
    7. Logging
      1. When and Why to Use Logs
      2. Writing to the Logs
      3. Log Channels
    8. Full-Text Search with Laravel Scout
      1. Installing Scout
      2. Marking Your Model for Indexing
      3. Searching Your Index
      4. Queues and Scout
      5. Performing Operations Without Indexing
      6. Conditionally Indexing Models
      7. Manually Triggering Indexing via Code
      8. Manually Triggering Indexing via the CLI
    9. Testing
      1. File Storage
      2. Session
      3. Cache
      4. Cookies
      5. Log
      6. Scout
    10. TL;DR
  16. 15. Mail and Notifications
    1. Mail
      1. “Classic” Mail
      2. Basic “Mailable” Mail Usage
      3. Mail Templates
      4. Methods Available in build()
      5. Attachments and Inline Images
      6. Markdown Mailables
      7. Rendering Mailables to the Browser
      8. Queues
      9. Local Development
    2. Notifications
      1. Defining the via() Method for Your Notifiables
      2. Sending Notifications
      3. Queueing Notifications
      4. Out-of-the-Box Notification Types
    3. Testing
      1. Mail
      2. Notifications
    4. TL;DR
  17. 16. Queues, Jobs, Events, Broadcasting, and the Scheduler
    1. Queues
      1. Why Queues?
      2. Basic Queue Configuration
      3. Queued Jobs
      4. Running a Queue Worker
      5. Handling Errors
      6. Controlling the Queue
      7. Queues Supporting Other Functions
    2. Laravel Horizon
    3. Events
      1. Firing an Event
      2. Listening for an Event
    4. Broadcasting Events over WebSockets, and Laravel Echo
      1. Configuration and Setup
      2. Broadcasting an Event
      3. Receiving the Message
      4. Advanced Broadcasting Tools
      5. Laravel Echo (the JavaScript Side)
    5. Scheduler
      1. Available Task Types
      2. Available Time Frames
      3. Defining Time Zones for Scheduled Commands
      4. Blocking and Overlap
      5. Handling Task Output
      6. Task Hooks
    6. Testing
    7. TL;DR
  18. 17. Helpers and Collections
    1. Helpers
      1. Arrays
      2. Strings
      3. Application Paths
      4. URLs
      5. Miscellaneous
    2. Collections
      1. The Basics
      2. A Few Methods
    3. Lazy Collections
    4. TL;DR
  19. 18. The Laravel Ecosystem
    1. Tools Covered in This Book
      1. Valet
      2. Homestead
      3. The Laravel Installer
      4. Mix
      5. Dusk
      6. Passport
      7. Horizon
      8. Echo
    2. Tools Not Covered in This Book
      1. Forge
      2. Vapor
      3. Envoyer
      4. Cashier
      5. Socialite
      6. Nova
      7. Spark
      8. Lumen
      9. Envoy
      10. Telescope
    3. Other Resources
  20. Glossary
  21. Index

Product Information

  • Title: Laravel: Up & Running, 2nd Edition
  • Author(s): Matt Stauffer
  • Release date: April 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492041207