久久久久久久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 重定向到登錄頁面)
主站蜘蛛池模板: 欧美国产精品 | 国产精品精品久久久久久 | 理论片午午伦夜理片影院 | 国产精品久久久久久久久免费丝袜 | 精品久久久久久红码专区 | 亚洲精品视频在线观看视频 | 91亚洲免费 | 日本成人中文字幕 | 高清av一区 | 国产传媒毛片精品视频第一次 | 亚洲国产情侣自拍 | 黄色网址av | 久草视频网站 | 中文字幕在线观看av | 久久免费精品 | av片免费 | 精品久久久久久久久久久久 | 欧美一区二 | 亚洲人成人一区二区在线观看 | 欧美性视频在线播放 | 一区日韩 | 久久精品国产免费一区二区三区 | www.黄色网| 免费视频成人国产精品网站 | 国产日韩免费视频 | 一a级片 | 国产福利91精品一区二区三区 | 亚洲成人一区二区三区 | 97精品超碰一区二区三区 | 成人免费观看网站 | 91视频一88av| 在线视频a | 欧美日韩在线一区二区三区 | 国产一区二区在线免费观看 | 日本免费在线 | 亚洲一区中文 | 日本午夜免费福利视频 | 在线播放国产一区二区三区 | 亚洲视频免费观看 | 欧美大片一区二区 | 国产91av视频在线观看 |