在Laravel控制器写两个方法,一个是在内部创建一个闭包函数,一个是执行传过来的闭包函数,测试闭包的写法,use使用外部变量,及闭包函数的传参。如下:
| 代码如下 | 复制代码 |
//测试闭包传参及use使用外部变量 publicfunctiontestClosure($t1,$t2) { $closure=function($param1,$param2)use($t1,$t2) { echo$param1.$param2.$t1.$t2; }; $this->execClosure('test.closure',$closure); } //执行闭包函数 protectedfunctionexecClosure($name, Closure$closure) { echo'Closure func name:'.$name; echo' $closure('p1','p2'); } | |
在routes.php添加路由:
| 代码如下 | 复制代码 |
| Route::get('/test/closure/{t1}/{t2}',['uses'=>'TestController@testClosure']); | |
访问www.example.com/test/closure/hehe1/hehe2
浏览器输出结果:
Closure func name:test.closure
p1p2hehe1hehe2
摩托车销售模拟器内置菜单中文版
摩托车出售模拟器,又名摩托车销售模拟器,这是一个以摩托车销售
船舶模拟2020最新版
船舶模拟2020是玩法非常有意思的模拟驾驶游戏,高清3d画质
油管主播的生活2内置菜单版
油管主播的生活2内置菜单版是一款模拟养成类游戏,在这里你将体
网吧老板模拟器2手机版
网吧老板模拟器2是非常受欢迎的模拟经营题材手游,在原本的基础
美国警察模拟器巡警2024最新版
美国警察模拟器是一款好玩的模拟手游,游戏中你将扮演一位警察,