久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 中的閉包是什么?

What is Closure in Laravel?(Laravel 中的閉包是什么?)
本文介紹了Laravel 中的閉包是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我在中間看到了一個 Laravel 函數(shù):

I saw one Laravel function in middlewere:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check())
    {
       return redirect('/home');
    } 

    return $next($request);
}

什么是Closure,它有什么作用?

What is Closure and what does it do?

推薦答案

A 關(guān)閉 是一個匿名函數(shù).閉包通常用作回調(diào)方法,并且可以用作函數(shù)中的參數(shù).

A Closure is an anonymous function. Closures are often used as callback methods and can be used as a parameter in a function.

如果你看下面的例子:

function handle(Closure $closure) {
    $closure();
}

handle(function(){
    echo 'Hello!';
});

我們首先在 handle 函數(shù)中添加一個 Closure 參數(shù).這將提示我們 handle 函數(shù)接受一個 Closure.

We start by adding a Closure parameter the handle function. This will type hint us that the handle function takes a Closure.

然后我們調(diào)用 handle 函數(shù)并傳遞一個函數(shù)作為第一個參數(shù).

We then call the handle function and pass a function as the first parameter.

通過在 handle 函數(shù)中使用 $closure(); 我們告訴 PHP 執(zhí)行給定的 Closure 然后 echo'你好!'

By using $closure(); in the handle function we tell PHP to execute the given Closure which will then echo 'Hello!'

也可以將參數(shù)傳遞到 Closure.我們可以通過更改 handle 函數(shù)中的 Closure 調(diào)用來傳遞參數(shù)來實現(xiàn).在這個例子中,我將只傳遞一個字符串,但這可以是任何變量.

It is also possible to pass parameters into a Closure. We can do so by changing the Closure call in the handle function to pass on a parameter. In this example i'll just pass a string but this can be any variable.

handle 函數(shù)現(xiàn)在看起來像

The handle function now looks like

function handle(Closure $closure) {
    $closure('Hello World!');
}

我們現(xiàn)在還需要修改 Closure 本身以獲取參數(shù).我們通過簡單地向函數(shù)添加一個參數(shù)來實現(xiàn).然后我們將該變量傳遞給 echo.

We now also need to modify the Closure itself to take the parameter. We do so by simply adding a parameter to the function. And then we pass that variable to the echo.

函數(shù)現(xiàn)在看起來像

handle(function($value){
    echo $value;
});

哪個將回顯 Hello World!

有關(guān)更多信息,您可以查看以下鏈接:

For more information you can check out these links:

http://php.net/manual/en/functions.anonymous.php

http://php.net/manual/en/class.closure.php

這篇關(guān)于Laravel 中的閉包是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創(chuàng)建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數(shù)據(jù)))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創(chuàng)建自定義輔助函數(shù)的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 激情五月婷婷 | 激情欧美日韩一区二区 | 高清国产午夜精品久久久久久 | 精品96久久久久久中文字幕无 | 久久综合香蕉 | 日韩不卡一二区 | 日韩一区二区在线看 | 国产精品1区 | 日韩中文在线观看 | www.蜜桃av | 最新黄色在线观看 | 一区在线视频 | 一区二区免费 | 黄色一级免费观看 | 成人在线精品视频 | 欧美一区二区 | 国产在线不卡视频 | 91久久久久久久久久久久久 | 国产在线高清 | 91精品国产91久久久久福利 | 国产在线网址 | 亚洲精品久久久久久一区二区 | 日韩精品一区二区三区视频播放 | 亚洲欧美激情国产综合久久久 | 精品乱码一区二区 | 黄网站免费入口 | 国产高清精品一区二区三区 | 天天干天天爱天天爽 | 午夜欧美| 亚洲精品视频一区二区三区 | 一级毛片成人免费看a | 欧美 日韩 国产 一区 | 中文字幕亚洲视频 | 综合久久av| 久久国产精品一区 | 午夜国产一级 | 日本a级大片 | 在线观看第一区 | 久久中文字幕视频 | 国产午夜精品久久久 | 日韩中文字幕区 |