Laravel: Up and Running

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. With this practical guide, Matt Stauffer—a leading teacher and developer in the Laravel community—provides the definitive introduction to one of today’s most popular web frameworks.

The book’s high-level overview and concrete examples will 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.

Dive into several features of this framework, including:

  • Blade, Laravel’s powerful, custom templating tool
  • Tools for gathering, validating, normalizing, and filtering user-provided data
  • Laravel’s Eloquent ORM for working with the application’s databases
  • The Illuminate request object, and its role in the application lifecycle
  • PHPUnit, Mockery, and PHPSpec for testing your PHP code
  • Laravel’s tools for writing JSON and RESTful APIs
  • Interfaces for file system access, sessions, cookies, caches, and search
  • Tools for implementing queues, jobs, events, and WebSocket event publishing
  • Laravel’s specialty packages: Scout, Passport, Cashier, Echo, Elixir, Valet, and Socialite

Publisher resources

View/Submit Errata

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. Creating a New Laravel Project
      1. Installing Laravel with the Laravel Installer Tool
      2. Installing Laravel with Composer’s create-project Feature
    5. Laravel’s Directory Structure
      1. The Folders
      2. The Loose Files
    6. Configuration
    7. Up and Running
    8. Testing
    9. 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. Views
      1. Using View Composers to Share Variables with Every View
    4. Controllers
      1. Getting User Input
      2. Injecting Dependencies into Controllers
      3. Resource Controllers
    5. Route Model Binding
      1. Implicit Route Model Binding
      2. Custom Route Model Binding
    6. Route Caching
    7. Form Method Spoofing
      1. An Introduction to HTTP Verbs
      2. HTTP Verbs in Laravel
      3. HTTP Method Spoofing in HTML Forms
    8. CSRF Protection
    9. Redirects
      1. redirect()->to()
      2. redirect()->route()
      3. redirect()->back()
      4. Other Redirect Methods
      5. redirect()->with()
    10. Aborting the Request
    11. Custom Responses
      1. response()->make()
      2. response()->json() and ->jsonp()
      3. response()->download() and ->file()
    12. Testing
    13. 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
    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
    6. Testing
    7. TL;DR
  6. 5. Frontend Components
    1. Elixir
      1. Elixir Folder Structure
      2. Running Elixir
      3. What Does Elixir Provide?
    2. Pagination
      1. Paginating Database Results
      2. Manually Creating Paginators
    3. Message Bags
      1. Named Error Bags
    4. String Helpers, Pluralization, and Localization
      1. The String Helpers and Pluralization
      2. Localization
    5. Testing
      1. Testing with Elixir
      2. Testing Message and Error Bags
      3. Translation and Localization
    6. 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() and $request->exists()
      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() in the Controller Using ValidatesRequests
      2. Manual Validation
      3. 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. Registering Commands
      2. A Sample Command
      3. Arguments and Options
      4. Using Input
      5. Prompts
      6. Output
    4. Calling Artisan Commands in Normal Code
    5. Tinker
    6. Testing
    7. 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
    4. Auth::routes()
    5. The Auth Scaffold
    6. “Remember Me”
    7. Manually Authenticating Users
    8. Auth Middleware
    9. Guards
      1. Changing the Default Guard
      2. Using Other Guards Without Changing the Default
      3. Adding a New Guard
      4. Creating a Custom User Provider
      5. Custom User Providers for Nonrelational Databases
    10. Auth Events
    11. Authorization (ACL) and Roles
      1. Defining Authorization Rules
      2. The Gate Facade (and Injecting Gate)
      3. The Authorize Middleware
      4. Controller Authorization
      5. Checking on the User Instance
      6. Blade Checks
      7. Intercepting Checks
      8. Policies
    12. Testing
    13. 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. Testing
    6. 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
    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. WithoutMiddleware
      2. DatabaseMigrations
      3. DatabaseTransactions
    5. Application Testing
      1. TestCase
      2. “Visiting” Routes
      3. Custom Application Testing Assertions
      4. JSON and Non-visit() Application Testing Assertions
      5. Clicking and Forms
      6. Jobs and Events
      7. Authentication and Sessions
    6. Artisan and Seed
    7. Mocking
      1. Mockery
      2. Mocking Facades
    8. 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
    7. Nesting and Relationships
    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
    9. Laravel 5.2+ API Token Authentication
    10. Testing
    11. 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. Sessions
      1. Accessing the Session
      2. The Methods Available on Session Instances
      3. Flash Session Storage
    4. Cache
      1. Accessing the Cache
      2. The Methods Available on Cache Instances
    5. Cookies
      1. Cookies in Laravel
      2. Accessing the Cookie Tools
    6. 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. Manually Trigger Indexing via Code
      7. Manually Trigger Indexing via the CLI
    7. Testing
      1. File Storage
      2. Session
      3. Cache
      4. Cookies
    8. 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. Queues
      7. 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. Events
      1. Firing an Event
      2. Listening for an Event
    3. 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)
    4. Scheduler
      1. Available Task Types
      2. Available Time Frames
      3. Blocking and Overlap
      4. Handling Task Output
      5. Task Hooks
    5. Testing
    6. 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
  19. Glossary
  20. Index

Product information

  • Title: Laravel: Up and Running
  • Author(s): Matt Stauffer
  • Release date: November 2016
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491936085