Head First Python , Second Edition by Paul Barry 2016年出版 英文版
Head First Python, Second Edition
by Paul Barry
Table of Contents (Summary)
1 The Basics: Getting Started Quickly 1
2 List Data: Working with Ordered Data 47
3 Structured Data: Working with Structured Data 95
4 Code Reuse: Functions and Modules 145
5 Building a Webapp: Getting Real 195
6 Storing and Manipulating Data: Where to Put Your Data 243
7 Using a Database: Putting Python’s DB-API to Use 281
8 A Little Bit of Class: Abstracting Behavior and State 309
9 The Context Management Protocol: Hooking into Python’s with Statement 335
10 Function Decorators: Wrapping Functions 363
11 Exception Handling: What to Do When Things Go Wrong 413
113⁄4 A Little Bit of Threading: Dealing with Waiting 461 12 Advanced Iteration: Looping like Crazy 477
A Installing: Installing Python 521
B Pythonanywhere: Deploying Your Webapp 529
C Top Ten Things We Didn’t Cover: There’s Always More to Learn 539
D Top Ten Projects Not Covered: Even More Tools, Libraries, and Modules 551
E Getting Involved: The Python Community 563
Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley
Foundations for Analytics with Python
by Clinton W. Brownley
Copyright © 2016 Clinton Brownley. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
Overview of Chapters
Chapter 1, Python Basics
We’ll begin by exploring how to create and run a Python script. This chapter focuses on basic Python syntax and the elements of Python that you need to know for later chapters in the book. For example, we’ll discuss basic data types such as numbers and strings and how you can manipulate them. We’ll also cover
Preface | xvii
the main data containers (i.e., lists, tuples, and dictionaries) and how you use them to store and manipulate your data, as well as how to deal with dates, as dates often appear in business analysis. This chapter also discusses programming concepts such as control flow, functions, and exceptions, as these are important elements for including business logic in your code and gracefully handling errors. Finally, the chapter explains how to get your computer to read a text file, read multiple text files, and write to a CSV-formatted output file. These are important techniques for accessing input data and retaining specific output data that I expand on in later chapters in the book.
Chapter 2, Comma-Separated Values (CSV) Files
This chapter covers how to read and write CSV files. The chapter starts with an example of parsing a CSV input file “by hand,” without Python’s built-in csv module. It transitions to an illustration of potential problems with this method of parsing and then presents an example of how to avoid these potential problems by parsing a CSV file with Python’s csv module. Next, the chapter discusses how to use three different types of conditional logic to filter for specific rows from the input file and write them to a CSV output file. Then the chapter presents two dif‐ ferent ways to filter for specific columns and write them to the output file. After covering how to read and parse a single CSV input file, we’ll move on to discus‐ sing how to read and process multiple CSV files. The examples in this section include presenting summary information about each of the input files, concate‐ nating data from the input files, and calculating basic statistics for each of the input files. The chapter ends with a couple of examples of less common proce‐ dures, including selecting a set of contiguous rows and adding a header row to the dataset.
Chapter 3, Excel Files
Next, we’ll cover how to read Excel workbooks with a downloadable, add-in module called xlrd. This chapter starts with an example of introspecting an Excel workbook (i.e., presenting how many worksheets the workbook contains, the names of the worksheets, and the number of rows and columns in each of the worksheets). Because Excel stores dates as numbers, the next section illustrates how to use a set of functions to format dates so they appear as dates instead of as numbers. Next, the chapter discusses how to use three different types of condi‐ tional logic to filter for specific rows from a single worksheet and write them to a CSV output file. Then the chapter presents two different ways to filter for specific columns and write them to the output file. After covering how to read and parse a single worksheet, the chapter moves on to discuss how to read and process all worksheets in a workbook and a subset of worksheets in a workbook. The exam‐ ples in these sections show how to filter for specific rows and columns in the worksheets. After discussing how to read and parse any number of worksheets in a single workbook, the chapter moves on to review how to read and process mul‐tiple workbooks. The examples in this section include presenting summary infor‐ mation about each of the workbooks, concatenating data from the workbooks, and calculating basic statistics for each of the workbooks. The chapter ends with a couple of examples of less common procedures, including selecting a set of contiguous rows and adding a header row to the dataset.
Chapter 4, Databases
Here, we’ll cover how to carry out basic database operations in Python. The chapter starts with examples that use Python’s built-in sqlite3 module so that you don’t have to install any additional software. The examples illustrate how to carry out some of the most common database operations, including creating a database and table, loading data in a CSV input file into a database table, updat‐ ing records in a table using a CSV input file, and querying a table. When you use the sqlite3 module, the database connection details are slightly different from the ones you would use to connect to other database systems like MySQL, Post‐ greSQL, and Oracle. To show this difference, the second half of the chapter dem‐ onstrates how to interact with a MySQL database system. If you don’t already have MySQL on your computer, the first step is to download and install MySQL. From there, the examples mirror the sqlite3 examples, including creating a database and table, loading data in a CSV input file into a database table, updat‐ ing records in a table using a CSV input file, querying a table, and writing query results to a CSV output file. Together, the examples in the two halves of this chapter provide a solid foundation for carrying out common database operations in Python.
Chapter 5, Applications
This chapter contains three examples that demonstrate how to combine techni‐ ques presented in earlier chapters to tackle three different problems that are rep‐ resentative of some common data processing and analysis tasks. The first application covers how to find specific records in a large collection of Excel and CSV files. As you can imagine, it’s a lot more efficient and fun to have a computer search for the records you need than it is to search for them yourself. Opening, searching in, and closing dozens of files isn’t fun, and the task becomes more and more challenging as the number of files increases. Because the problem involves searching through CSV and Excel files, this example utilizes a lot of the material covered in Chapters 2 and 3.
The second application covers how to group or “bin” data into unique categories and calculate statistics for each of the categories. The specific example is parsing a CSV file of customer service package purchases that shows when customers paid for particular service packages (i.e., Bronze, Silver, or Gold), organizing the data into unique customer names and packages, and adding up the amount of time each customer spent in each package. The example uses two building blocks, creating a function and storing data in a dictionary, which are introduced
in Chapter 1 but aren’t used in Chapters 2, 3, and 4. It also introduces another new technique: keeping track of the previous row you processed and the row you’re currently processing, in order to calculate a statistic based on values in the two rows. These two techniques—grouping or binning data with a dictionary and keeping track of the current row and the previous row—are very powerful capabilities that enable you to handle many common analysis tasks that involve events over time.
The third application covers how to parse a text file, group or bin data into cate‐ gories, and calculate statistics for the categories. The specific example is parsing a MySQL error log file, organizing the data into unique dates and error messages, and counting the number of times each error message appeared on each date. The example reviews how to parse a text file, a technique that briefly appears in Chapter 1. The example also shows how to store information separately in both a list and a dictionary in order to create the header row and the data rows for the output file. This is a reminder that you can parse text files with basic string oper‐ ations and another good example of how to use a nested dictionary to group or bin data into unique categories.
Chapter 6, Figures and Plots
In this chapter, you’ll learn how to create common statistical graphs and plots in Python with four plotting libraries: matplotlib, pandas, ggplot, and seaborn. The chapter begins with matplotlib because it’s a long-standing package with lots of documentation (in fact, pandas and seaborn are built on top of matplot lib). The matplotlib section illustrates how to create histograms and bar, line, scatter, and box plots. The pandas section discusses some of the ways pandas simplifies the syntax you need to create these plots and illustrates how to create them with pandas. The ggplot section notes the library’s historical relationship with R and the Grammar of Graphics and illustrates how to use ggplot to build some common statistical plots. Finally, the seaborn section discusses how to cre‐ ate standard statistical plots as well as plots that would be more cumbersome to code in matplotlib.
Chapter 7, Descriptive Statistics and Modeling
Here, we’ll look at how to produce standard summary statistics and estimate regression and classification models with the pandas and statsmodels packages. pandas has functions for calculating measures of central tendency (e.g., mean, median, and mode), as well as for calculating dispersion (e.g., variance and stan‐ dard deviation). It also has functions for grouping data, which makes it easy to calculate these statistics for different groups of data. The statsmodels package has functions for estimating many types of regression and classification models. The chapter illustrates how to build multivariate linear regression and logistic classification models based on data in pandas DataFrames and then use the mod‐ els to predict output values for new input data.
Chapter 8, Scheduling Scripts to Run Automatically
This chapter covers how to schedule your scripts to run automatically on a rou‐ tine basis on both Windows and macOS. Until this chapter, we ran the scripts manually on the command line. Running a script manually on the command line is convenient when you’re debugging the script or running it on an ad hoc basis. However, it can be a nuisance if your script needs to run on a routine basis (e.g., daily, weekly, monthly, or quarterly), or if you need to run lots of scripts on a routine basis. On Windows, you create scheduled tasks to run scripts automati‐ cally on a routine basis. On macOS, you create cron jobs, which perform the same actions. This chapter includes several screenshots to show you how to cre‐ ate and run scheduled tasks and cron jobs. By scheduling your scripts to run on a routine basis, you don’t ever forget to run a script and you can scale beyond what’s possible when you’re running scripts manually on the command line.
Chapter 9, Where to Go from Here
The final chapter covers some additional built-in and add-in Python modules and functions that are important for data processing and analysis tasks, as well as some additional data structures that will enable you to efficiently handle a variety of complex programming problems you may run into as you move beyond the topics covered in this book. Built-ins are bundled into the Python installation, so they are immediately available to you when you install Python. The built-in mod‐ ules discussed in this chapter include collections, random, statistics, iter tools, and operator. The built-in functions include enumerate, filter, reduce, and zip. Add-in modules don’t come with the Python installation, so you have to download and install them separately. The add-in modules discussed in this chapter include NumPy, SciPy, and Scikit-Learn. We also take a look at some additional data structures that can help you store, process, or analyze your data more quickly and efficiently, such as stacks, queues, trees, and graphs.
Laraboot: Laravel 5 For Beginners
Laravel 5.2 初学者指南
Laraboot: Laravel 5 For Beginners
Bill Keck
This book is for sale at http://leanpub.com/laravel-5-for-beginners-laraboot
This version was published on 2016-04-04
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
© 2015 - 2016 Bill Keck
Contents
Chapter 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Makes The Laravel Framework Special? . . . . . . . . . . . . . . . . . . . . . . . . 3
Upsides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Downsides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Why I chose Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Why I’m Writing This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Artisan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
MySql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Minimum PHP Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
W3 Schools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
MInimum HTML and CSS skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Minimum Javascript Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Contact Bill Keck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Laraboot on Github . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Chapter 2: The Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Fun Part . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
MAC or Windows? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
MAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Minimum Version of PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Homebrew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Command Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 3: Installation of Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Composer install of Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CONTENTS
Create Project in IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Setup Local Host file: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Vhost Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
httpd.conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
MOD Rewrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Restart Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Chapter 4: Let’s Get Started With Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Set Up The Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Initial Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Diving Into Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Setup the DB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
.env . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Application Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Basic Stitching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Style Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Creating a Route . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Creating a Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Artisan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
RESTful pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Namespaces and Use Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Index Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Blade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Creating A Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
CDN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
View Partials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Chapter 5: User Registration And Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
User Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Migrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Installing the Dbal Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Make Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
AuthController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Gravatar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Facades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
CONTENTS
Bootstrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Pages Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
New Nav . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Auth Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Auth Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
emails view folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
password.blade.php. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Config Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
passwords view folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
email.blade.php view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
reset.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Auth View Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
register.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
login.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Chapter 6: Working with the RESTful Pattern . . . . . . . . . . . . . . . . . . . . . . . . . 121
Sweet Alert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Model Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Route Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
RESTful Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Collective HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Errors List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Store Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Die and Dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Index Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Datatables Jquery Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Datatables CDN Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Datatables CSS CDN Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The API Route . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Api/Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Test the API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Index View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Datatable Partial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Datatable-script Partial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Slugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Changing the Store Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Create the Slug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Changing the Create Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Add Auth Use Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
CONTENTS
Change $fillable Property on Widget Model . . . . . . . . . . . . . . . . . . . . . . . . . 171
Basic Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Change widgetData Method on ApiController . . . . . . . . . . . . . . . . . . . . . . . . 173
Change datatable.blade Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Change datatable-script.blade.php to Add Columns . . . . . . . . . . . . . . . . . . . . . 174
Add Middleware to WidgetController . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Change Routes to Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Show Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Accessors and Mutators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Edit Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Update Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Destroy Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
404.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Nav . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Chapter 7: Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
OwnsRecord Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Modify User Table Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
User $fillable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Admin Middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
AllowIfAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
isAdmin Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Admin Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
NoActiveAccountException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Modifying the Auth Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
AuthController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Update Users Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Registration Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
register.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Users Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Update User $fillable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
AuthController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Create Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Terms Of Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Privacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Chapter 8: Socialite - One Click Facebook Login . . . . . . . . . . . . . . . . . . . . . . . . 261
Facebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Modify Users Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
CONTENTS
Mass Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Social Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
session.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
InvalidStateException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Set up Facebook App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Step 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Step 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Step 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Step 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Step 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Step 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Step 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Step 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Step 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Step 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Step 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Step 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Step 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Integrating Socialite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
AuthController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Navigation To Facebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Chapter 9: Profile, Settings and Admin Dash . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
my-profile and show-profile Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Add Profile to Nav . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Profile Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Show View - Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Index View - Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Edit View - Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
API Route for Profile: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
profileData Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Datatable-script - Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Datatable View - Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Users For Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
UserController.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
UserRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
User Model changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
HasModelTrait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Index View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Datatable View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Datatable-script View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
CONTENTS
Show View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Edit View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Navigation to Users & Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Settings Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
SettingsController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Create Settings View Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Edit View For Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Add Settings To Nav . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Admin Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Add Facebook sign in buttons to Login and Register Views . . . . . . . . . . . . . . . . . 388
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Chapter 10: Working With Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Create imgs, marketing-images, and thumbnails folder. . . . . . . . . . . . . . . . . . . . 392
Create Image Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Show Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Add Accessor Methods to MarketingImage Model . . . . . . . . . . . . . . . . . . . . . . 423
show view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Edit view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Edit Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Edit Image Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Update method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Destroy Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Api route . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
API Controller Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Index method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
index view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
datatable view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
datatable-script.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Add Nav To Marketing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Carousel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Pages Index View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Update slider.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
image_weight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Modify MarketingImageController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
CreateImageRequest and EditImageRequest . . . . . . . . . . . . . . . . . . . . . . . . . 463
Marketing Image Create and edit Views . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
MarketingImage Show View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
marketingImageData API Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Datatable View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
datatable-script.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
PagesController Index Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
CONTENTS
grid.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
nav.blade.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Chapter 11: Making a Custom Artisan Command . . . . . . . . . . . . . . . . . . . . . . . 470
Make Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Handle Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
makeTraitDirectory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Learning Laravel's Eloquent
What this book covers
Chapter 1, Setting Up Our First Project, will discuss how to deal with Composer
and Homestead. We will also cover the installation process of our very first
Laravel project.
Chapter 2, Building the Database with the Schema Builder Class, will discuss the Schema
Builder Class. We will analyze everything you can do with the class, look at different
types of indexing, and learn about the methods that the Schema class provides.
[ v ]Preface
Chapter 3, The Most Important Element – the Model!, will help us implement some
"create," "read," "update," and "delete" logic for our items. We will also explore
some useful methods and features of the model class.
Chapter 4, Exploring the World of Relationships, will help us discover how to work with
different types of relationships and how to query and use them in a comfortable
and clean way. Also, we will learn how to insert and delete related models in our
database, or update existing ones.
Chapter 5, Using Collections to Enhance Results, will talk about collections. We will
work with some results transformation methods and with the elements that make
up a collection.
Chapter 6, Everything under Control with Events and Observers, will allow us to learn
everything about the events in the context of Eloquent models. Right after, we will
cover model events and model observers.
Chapter 7, Eloquent... without Laravel!, will explore the structure of the database
package and see what is inside it. After that, we will learn how to install the
"illuminate/database" package separately for your project and how to configure it
for its first use. Yes, exactly: Eloquent without Laravel!
Chapter 8, It's Not Enough! Extending Eloquent, Advanced Concepts, will explore
two different ways to extend Eloquent, and move on to learn about the
Repository pattern.
SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版
Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065)
1 Declarations and Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
5 Flow Control, Exceptions, and Assertions . . . . . . . . . . . . . . . . . . . . 327
6 Strings, I/O, Formatting, and Parsing . . . . . . . . . . . . . . . . . . . . . . . . 425
7 Generics and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
8 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
9 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
10 Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789