Home >Backend Development >PHP Tutorial >Laravel 5 documentation reading summary

Laravel 5 documentation reading summary

WBOY
WBOYOriginal
2016-08-10 08:48:38884browse

Laravel 5Project structure analysis and Chinese document reading summary

HTTProuting1

Middleware 5

Controller5

HTTPRequest 7

http Service Providers11

Service Container12

Contracts13

Facades14

Request lifecycle15

Application structure16

Certification20

Cache24

Collection26

Artisan Console26

Extension Framework*27

Laravel Elixir*27

Encryption27

Errors and Logs28

Events28

File System / Cloud Storage30

Hash31

Auxiliary methods 31

Localization*31

Mail* 31

Expansion Pack Development*31

Pagination*31

Queue*31

Session32

BladeTemplate 33

Unit Test* 35

Data verification35

Basics of database usage 36

Query Constructor38

Structure Generator41

Migration and Data Population41

Eloquent ORM41

HTTP routing

Basic routing

Define routes for different Http Method, such as:

Route::get('/', function(){

Route: : post('foo/bar', function(){

Route::match(['get', 'post'], '/', function(){ # Multiple methods

Route::any('foo', function(){ # All methods

use the url method to generate url$url = url('foo');

CSRFprotection

Laravel will automatically place a random token in each user’s session. The VerifyCsrfToken middleware pairs the request saved in the session with the input token to verify the token. In addition to looking for the CSRF token as the "POST" parameter, the middleware also checks the X-XSRF-TOKEN request header.

Insert CSRF Token into the form:

in Blade template engine using :

added to the X-XSRF-TOKEN request header :

csrf_token()}}" />

$.ajaxSetup ({

headers: {

}

});

...

#

In this way, all ajax requests will carry this header information:

$.ajax({

url: "/foo/bar",

})

method cheat

Route parameters

Route::get('user/

{id} ', function($id){ #

Basic parameters

Route::get('user/{name?}', function($name = null){ #

Optional parameters

Route::get('user/{name?}', function(

$name = 'John'

){ # Parameters with default values You can define the global mode of parameters. Define the mode in the

boot

method of RouteServiceProvider

: $router-&g t;pattern('id ', '[0-9]+');

will be applied to all routes using this specific parameter:

Route::get('user/

{id}', function($id)

if (

$route->input('id')

== 1){ #

External to the route Get parameters

You can also get parameters through dependency injection:

use IlluminateHttp

Request;

Route::get('user/{ id}', function(Request $request, $id){

if ($request->route('id')){

Route naming

Route::get('user/profile', [

'as' => 'profile', function(){

#

Specify the route name for the controller action

Route::get('user/profile', [

'as' => 'profile',

'uses' => 'UserController@showProfile'

]);

# Use named routes for redirection

$url = route('profile');

$redirect = redirect ()-> route('profile');

# Returns the name of the current route request

$name = Route:: currentRouteName();

Route Group

Treat shared properties as an array as Route::groupFirst parameter:

# Shared middleware

Route::group(['middleware' => ['foo', 'bar']], function()

{

Route::get('/', function()

{

});

Route::get('user/profile', function()

{

// Has Foo And Bar Middleware

});

});

#

In the above example, foo and bar are the middleware key names. The mapping relationship between the key name and class name of the customized middleware needs to be added in Kernel.php.

#

Shared namespace

Route::group([

'namespace' => 'Admin'], function() ​​​​

'namespace'

=> 'User'], function ()

{

// Controllers Within The "AppHttpControllersAdminUser" Namespace

}) ;

});

Subdomain routing

Route::group(['domain' => '

{account}

.myapp.com'], function()

{

{

Route::get('user/{id}', function($account, $id)

{

                                           //

);

});

Route::group([

'prefix'

=> 'admin'], function()

{

Route::get('users', function()

{

// Matches The "/admin/users

" URL

});

});

# is defined in the routing prefix Parameters

Route::group(['prefix' => 'accounts/{account_id}

'], function()

{

Route ::get('detail', function($account_id)

                                                                 

Route model binding

model Binding provides a convenient way to inject model entities into routes

: Instead of injecting

User ID, you can choose to inject User

class entities that match the given ID

. Define model binding in the RouteServiceProvider::boot method:

public function boot(Router $router){ parent::boot($router ; {user} Parameter route:

Route::get('profile/{user}

', function(

AppUser $user){

) / /

});

A request to

profile/1 will inject the ID

for the 1 entityUser

. If the entity does not exist, throw 404. You can pass a closure as the third parameter to define the behavior when it is not found.

throws 404 error

Two methods:

abort (404); # Essentially throws a SymfonyComponentHttpKernelExceptionHttpException with a specific status code. Or: manually throw HttpException

middleware

New middleware

php artisan make:middleware OldMiddleware # Create a new middlewareThe main function of the middleware is implemented in the handle()

method:

class OldMiddleware {

public function handle($request, Closure $next){

if (xxx){

return

redirect('xx');

                                                                      🎙 }

Analyzing its structure, we can find that it is basically the execution Make a judgment, and then redirect or continue forward.

Global middleware

If you want the middleware to be executed by all HTTP requests, just add the middleware class to

app/Http/ Kernel.php

’s

$middleware property list.

Assign middleware to routing

After creating the new middleware, go to app/Http/Kernel.php

of

Add middle in $routeMiddleware The mapping relationship between the file key name and the class name, and then you can use this key name in routing to assign routes:

Route::get('admin/profile', ['middleware' =>

'auth', function(){

Terminable middleware

Terminable middleware needs to inherit from TerminableMiddleware

and implement terminate() Method. Its purpose is to execute after the HTTP response has been sent to the client. Terminable middleware needs to be added to the global middleware list of app/Http/Kernel.php .

Controller

Basic Controller

All controllers should extend the base controller class

use AppHttpControllers Controller;

class UserController extends Controller { # Inherits Controller

public function showProfile($id ) # Action

{

AppHttpControllersController is defined as follows:

namespace BorogadiHttpControllers;

use IlluminateFoundationBusDispatchesJobs;

use

IlluminateRoutingController

as BaseController;use IlluminateFoundationValidationValidatesRequests;

abstract class Controller extends BaseController

{

use DispatchesJobs, ValidatesRequests;

}It can be seen that it is ultimately inherited from the IlluminateRoutingController

class.

# Named controller route

Route::get('foo', ['uses' => 'FooController@method',

'as ' => 'name']);

# URL pointing to the controller

$url =

action('AppHttpControllersFooController@ method');

or:

URL::setRootControllerNamespace

('AppHttpControllers');

$url =

action('FooController@

Controller middleware

Two ways, one is to specify in the controller route:

Route::get('profile', [

'middleware'

=> 'auth',

'uses' => 'UserController@showProfile'

]);

The other is Specify directly in the controller constructor:

class UserController extends Controller { public function __construct(){

$this->middleware

(' auth');

$this->middleware

('log', ['only' => ['fooAction', 'barAction']]);

Implicit controller

Implicit controller implementation defines a single route to handle each behavior in the controller:

Define a route:

Route ::controller('users', 'UserController');

Define the implementation of the controller class:

class UserController extends BaseController {

public function getIndex (){ # Response to

user

public function postProfile(){ # Response to

post way user/profile

Public function anyLogin(){ # Response to all methods of user/login

It is possible to support multi-word controller behavior by using "-": public function getAdminProfile() {} # Response to users/admin-profile , not user/adminprofile. Pay attention to the camel case naming method used in the action name

RESTfulResource controller

is actually a specific application of implicit controller.

Route cache

If only controller routing is used in the application, route caching can be used to improve performance.

php artisan route:cache

The cache route file will be used instead of the app/Http/routes.php file

HTTP request

Get the request

in two ways, one is through the Request facade:

use Request;

$name = Request::input('name');

or via dependency injection: use type hints on the class in the constructor or method in the controller. The currently requested instance will be automatically injected by the service container:

use IlluminateHttpRequest;

use IlluminateRoutingController;

class UserController extends Controller {

public function store(Request$request){

                                                   $name                                                                                                ​; request, $id

){

Get input data

$name = Request::input('name') ; # Get specific input data

$name = Request::input('name', 'Sally'); # Get specific input data, if not, get the default value

if (Request::has('name')){ # Confirm whether there is input data

$input = Request::all

();

# Get all input data$input = Request::only

('username', 'password');

# Get some input data $input = Request::except

('credit_card');

# Get partial input data exclusion method$input = Request::input('products.0.name'); # Get data in array form

old input data

Request::flash(); # Save the current input data into

session

Request::

flashOnly('username', 'email'); # Save some data into session Request::

flashExcept

('password'); # Save some data as session, elimination method

return redirect('form')->

withInput(); # Redirect and cache the current input data to session

return redirect('form')->withInput(Request::except('password')); # Redirect and cache part of the current input data to session

$username = Request::old('username'); # Get the one-time Session

{{ old(' username') }} # Showing old input data in bladetemplate

Cookies

L cookie created by aravel Encrypt and add authentication mark.

$value = Request::cookie('name'); # Get the Cookievalue

#In response Add Cookies

$response = new IlluminateHttpResponse('Hello World');

$response->withCookie (cookie('name', 'value' , $minutes));

$response->withCookie(cookie()->forever('name', 'value')); # Add Permanent Cookie

# Add Cookie in queue mode, that is, set Cookie before actually sending the response

Cookie::queue ('name', 'value');

return response('Hello World'); $file = Request: ; :file('photo')->isValid()) # Confirm whether the uploaded file is valid

Request::file('photo')->move($destinationPath); # Move Uploaded file

Request::file('photo')->move($destinationPath, $fileName); # Move the uploaded file and rename it

Other request information

$uri = Request::path(); # Get request

URI

if (Request::ajax())

# Determine whether a request uses AJAX if (Request::isMethod('post'))

if (Request::is('admin/*'))

# Confirm whether the request path meets the specific format $url = Request::url(); #

Get request

URL

HTTP response

Basic response

Route ::get('/', function(){ # return string

return 'Hello World';

# Return to complete Responses

Instance, there are two methods

return Responses object:

use IlluminateHttpResponse;

return (new Response($content, $status))

- ->header('Content-Type', $value);

or use

response

auxiliary Method:

return response($content, $status)->header('Content-Type', $value);

# Return to view

return response()-> return response($content)->

withCookie(

cookie('name', 'value'));

Redirect return redirect('user/login');

# Use

redirect

redirect method

return redirect ('user/login')-> ;with('message', 'Login Failed'); # Redirect and save the current data to Sessionreturn redirect()->back

() ; # Redirect to previous locationreturn redirect()->

route('login'); # Redirect to specific route

# Redirect to a specific route with parameters return redirect()->route('profile', [1]); #

routed

The URI

is:

profile/{id}

return redirect()->route('profile', ['user' => 1]); # routed URI is: profile/{user}

# Redirect based on controller actionreturn redirect()->action

('AppHttpControllersHomeController@index');

return redirect()->action('AppHttpControllersUserController@profile', ['user' => 1]); # With parameters

Other responses

# returnjson

return response()-> ;json(['name' => 'Abigail', 'state' => 'CA']); >json([' name' => 'Abigail', 'state' => 'CA'])

- ->setCallback($request->input('callback'));

# File download

return response()->download($pathToFile, $name, $headers);

Response Macro

# Define the response macro, usually defined in the Provider method

Response::macro

('caps', function($value)

use

($response){ # PHPBy default, anonymous functions cannot call the context variables of the code block in which they are located, but need to use the

use keyword.

use will copy a copy of the variable into the closure, and also supports reference forms, such as use ( &$rmb )

return $response->make(strtoupper( $value));

});

# Call the response macroreturn response()- >caps(' foo');

ViewBasic view

# View definition File path and file name: resources/views/greeting.php

                                                                                                     

#

View call

Route::get('/', function()

{

  return view( 'greeting'

, [

'name' => 'James']);

#

The parameter passed to the view is an array of key-value pairs});

#

Subfolder view call Definition location: resources/views/

admin/profile.php

return view(' admin.profile ', $data);

# Other ways to pass data to the view

$view = view('greeting')->

with

( 'name', 'Victoria'); # Traditional method

$view = view('greeting')->withName('Victoria'); # Magic Method

$view = view('greetings', $data); # Directly pass the array $data is an array of key-value pairs

# Share data to all views

Customize a Provider

, or add directly in the

boot method of AppServiceProvider

:

view( )->share('data', [1, 2, 3]); or:

View::share('data', [1, 2 , 3]);

# Confirm whether the view exists

if (view()->exists

('emails .customer'))

# Generate a view from a file path

return view()->file

($pathToFile, $data);

View componentView component is a closure or class method that is called before the view is rendered.

#

Define view components

use View;

use IlluminateSupportServiceProvider; View::

composer

('profile', 'AppHttpViewComposersProfileComposer'); #指 Use a class to specify the view component

View ::

composer ('dashboard', function ($ view) {

# Use closure to specify the view component

...

                                                              Use classes to specify view components, The method named compose of the specified class will be called before the view is rendered. As in the above example, the

ProfileComposer'

class is defined as:

use IlluminateContractsViewView;

use IlluminateUsersRepository as UserRepository;

class ProfileComposer {

protected $users;

public function __construct(UserRepository $users){ # service container will automatically parse the required parameters

                                           

Public function

compose(View $view){

# The compose method is passed an instance of View, where parameters can be passed to View          $ view->with('count', $this->users->count());

}

}

#

Using wildcards within the view component

View::composer('*', function($view){ # is equivalent to defining it for all views

#

Attach view components to multiple views at the same time

View::composer(['profile', 'dashboard'], 'AppHttpViewComposersMyViewComposer');

#

Many definitions View components

View::composers([

'AppHttpViewComposersAdminComposer' => ['admin.index', 'admin.profile'],

'AppHttpViewComposersUserComposer ' => 'user',

'AppHttpViewComposersProductComposer' => 'product'

]);

Service Providers

Each custom Provider must inherit from

IlluminateSupport

ServiceProvider and be registered in the Providers array in config/app.php. The custom Provider must define the register() method, which is used to define the behavior during registration. In addition, there are two optional methods and an optional attribute: the boot() method will not be called until all Provider have been loaded, and the provides()method Used in conjunction with the $defer optional attribute to provide the buffering function. The idea of ​​​​providing services through service providers: implement a class that completes the actual work, define a Provider, and use it in the

register()

method of Provider Methods to register the actual work class with the system container and obtain the actual work class instance. Then register this Provider in the application configuration. In this way, the register() method of all Provider will be called when the application is initialized to indirectly register the way to obtain the actual working class instance. #

Define a basic

Provider

use RiakConnection;

use IlluminateSupportServiceProvider ) # Register a class in the container and obtain its instance Method

                                                                                                                              Return new Connection($app['config' ]['riak']);

                                             ; Service Container

Basic usage

in Inside the

Provider

, the service container can be accessed through

$this->app

.

There are two main ways to register dependencies: callback interface method and binding instance interface.

# The way of closure callback$this->app->bind('FooBar', function($app){

Return new FooBar($app['SomethingElse']);

});

# Registered as a singleton, subsequent calls will return the same instance

$this->app->singleton('FooBar', function($app){

return new FooBar($app['SomethingElse']);

});

#

Bind to an existing instance

$fooBar = new FooBar(new SomethingElse);

$this ->app->instance('FooBar', $fooBar);

There are two ways to resolve the instance from the container:

$fooBar = $this-> ;app->make('FooBar');

# Use the make()

method to parse

$fooBar = $this->app['FooBar'];

# Because the container implements the ArrayAccess

interface, you can use the array access form After defining the registration and parsing information, you can directly use it in the constructor of the class Specify the required dependencies via type-hint

, and the container will automatically inject all the required dependencies.

use IlluminateRoutingController; AppUsersRepository as UserRepository;class UserController extends Controller {

protected $users;

public function __construct(

UserRepository $users){

# type-hint

                                                                                                                              $users;

}

public function show($id){

}

}

Binding interface

interface

EventPusher

{

Public function push($event, array $data);

}

class

PusherEventPusher

implements EventPusher {

...

}

Because the PusherEventPusher

class implements EventPusher Interface, so you can directly register this interface and bind it to a class that implements this interface: $this->app->

bind

('AppContractsEventPusher', 'AppServicesPusherEventPusher ');

When a class requires the EventPusher interface, it will tell the container that it should inject

PusherEventPusher.

Context binding

$this->app->when

('AppHandlersCommandsCreateOrderHandler')

                                                                  needs('AppContractsEventPusher')                                                           tPusher');

tag

$this->app->bind('SpeedReport', function(){

});

$this->app->bind ('MemoryReport', function (){

}); reports');

#

Tag the class registered in the previous two steps as 'reports'

Parse them properly:

$this->app->bind('ReportAggregator', function($app){ ) return new ReportAggregator($app->tagged ('reports'));

});

Container events

The container will trigger an event when parsing each object. You can use the resolving method to listen for this event (the parsed object will be passed into the closure method):

$this->app->resolving(function($object, $app){ # Called when the container resolves any type of dependency

...

});$this->app- >resolving(function(FooBar $fooBar, $app){ #

Called when the container resolves a dependency of type

'FooBar'

...

});

Contracts

Contracts is the interface used by all

Laravel

main component implementations , you can see it under the Contracts

directory The directory structure is the same as in

Illuminate.

Contracts

is the interface definition,

Illuminate

is the specific implementation. Each concrete implemented class in Illuminate

extends its corresponding interface in

Contracts. This separation of interface and implementation can make dependency injection low-coupling. /laravel/framework/src

/Illuminate

/Auth

/Broadcasting

/Bus

...

/Contracts

/Auth

/Broadcasting

/Bus

...

FacadesBasic usage

Facades provide a static interface to classes that can be accessed in the application's

service container. (An application of "decoration mode" in design pattern mainly uses class_alias

to create category names, and also uses

__callStatic() to provide a static proxy, which is ultimately simulated using a mock object

PHP

object and call the object's method)

Laravel's facades and any custom facades you create, will inherit the base class Facade and only need to implement one method: getFacadeAccessor() .

Such as CacheThis facade is called: $value = Cache::get( 'key');

Look at the implementation of the class:

class Cache extends Facade {

protected static function getFacadeAccessor() { return 'cache'; } # This The function of the method is to return the name of the service container binding

}

When the user executes any static method on the Cache's facade, Laravel Will resolve the bound cache from the service container and execute the requested method (in this example, get)

on that object All facades exist in the global namespace. When used in a nested namespace, you need to import the facade class into the namespace:

use Cache; # ImportCache facade

class PhotosController extends Controller {

public function index(){

$ photos = Cache::get('photos');

}

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn