O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Laravel: Up and 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 vast ecosystem of tools let you quickly build new sites and applications with clean, readable code. Fully updated to cover Laravel 5.6, the latest release, the second edition of this practical guide provides the definitive introduction to one of today’s most popular 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 Laravel right away. By the time you reach the last page, you should feel comfortable writing an entire application in Laravel from scratch.

Table of Contents

  1. Preface
    1. What This Book Is About
    2. Who This Book Is For
    3. How This Book Is Structured
    4. Conventions Used in This Book
    5. O’Reilly Safari
    6. How to Contact Us
    7. Acknowledgments
  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
    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. Vessel
    5. Creating a New Laravel Project
      1. Installing Laravel with the Laravel Installer Tool
      2. Installing Laravel with Composer’s create-project Feature
    6. Super-powered “Laravel New”
    7. Laravel’s Directory Structure
      1. The Folders
      2. The Loose Files
    8. Configuration
      1. The .env file
    9. Up and Running
    10. Testing
    11. TL;DR
  4. 3. Routing and Controllers
    1. Route Definitions
      1. Route Verbs
      2. Route Handling
      3. Route Parameters
      4. Route Names
    2. Route Groups
      1. Middleware
      2. Path Prefixes
      3. Subdomain Routing
      4. Namespace Prefixes
      5. Name Prefixes
    3. Signed routes
      1. Signing a route
      2. Modifying routes to allow signed links
    4. Views
      1. Using View Composers to Share Variables with Every View
    5. Controllers
      1. Getting User Input
      2. Injecting Dependencies into Controllers
      3. Resource Controllers
      4. API resource controllers
      5. Single Action Controllers
    6. Route Model Binding
      1. Implicit Route Model Binding
      2. Custom Route Model Binding
    7. Route Caching
    8. Form Method Spoofing
      1. An Introduction to HTTP Verbs
      2. HTTP Verbs in Laravel
      3. HTTP Method Spoofing in HTML Forms
    9. CSRF Protection
    10. Redirects
      1. redirect()->to()
      2. redirect()->route()
      3. redirect()->back()
      4. Other Redirect Methods
      5. redirect()->with()
    11. Aborting the Request
    12. Custom Responses
      1. response()->make()
      2. response()->json() and ->jsonp()
      3. response()->download(), ->streamDownload(), and ->file()
    13. Testing
    14. TL;DR
  5. 4. Blade Templating
    1. Echoing Data
    2. Control Structures
      1. Conditionals
      2. Loops
      3. or
    3. Template Inheritance
      1. Defining Sections with @section/@show and @yield
      2. @parent
      3. @include
      4. @each
      5. 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. Frontend Components
    1. Mix
      1. Mix Folder Structure
      2. Running Mix
      3. What Does Mix Provide?
    2. 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
  7. 6. 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. Array Input
      6. 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
  8. 7. 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
  9. 8. Database and Eloquent
    1. Configuration
      1. Database Connections
      2. 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
  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. Manually Authenticating Users
    8. Manually Logging Out a user
      1. Invalidating Sessions On Other Devices
    9. Auth Middleware
    10. Email Verification
    11. Blade Authentication Directives
    12. 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
    13. Auth Events
    14. 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
    15. Testing
    16. TL;DR
  11. 10. Requests and Responses
    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. Persistence
    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
    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
    8. Database Tests
      1. Using model factories in tests
    9. Testing other Laravel systems
      1. Event Fakes
      2. Mail Fakes
      3. Notification Fakes
      4. Queue Fakes
      5. Bus Fakes
      6. Storage Fakes
      7. Faking other Facades
      8. Testing with Jobs and Events (but not fakes)
    10. Mocking
      1. A quick introduction to mocking
      2. A quick introduction to Mockery
    11. Testing Artisan commands
    12. Browser Tests
      1. Application Tests using Laravel 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 the Vue Components
      6. Passport Scopes
      7. Deploying Passport
    9. API Token Authentication
    10. Customizing 404 Responses
      1. Triggering the fallback route on a ModelNotFoundException
    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. The Methods Available on Session Instances
      3. Flash Session Storage
    5. Cache
      1. Accessing the Cache
      2. The 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
      4. Writing to specific log channels
      5. Advanced log configuration
    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. Perform Operations Without Indexing
      6. Conditionally Indexing Models
      7. Manually Trigger Indexing via Code
      8. Manually Trigger Indexing via the CLI
    9. Testing
      1. File Storage
      2. Session
      3. Cache
      4. Cookies
    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. Blocking and Overlap
      4. Handling Task Output
      5. Task Hooks
    6. Testing
    7. TL;DR
  18. 17. Helpers and Collections
    1. Helpers
      1. Arrays
      2. Strings
      3. Application Paths
      4. URLs
      5. Misc
    2. Collections
      1. The Basics of Collections
      2. A Few Methods
    3. TL;DR