如何定义高级函数(如laravel组件)


How to define advance function (like laravel components)

我有两个问题:
Q1.我有一个功能:

namespace Core;
class MyRoute {
  static function get($slug, $action) {
   // code  
  }
}

我想像laravel路由器一样定义这个函数:

$checkLogin = function() {
    if(Sentry::check()) return false;
    return true;
};
MyRoute::get('foo/bar', function() {
  //code
})->before($checkLogin);

如何获得$action和句柄?

Q2:我想使用Silex应用程序(路由),我想使用我的静态类,回调到$app

namespace Core;
class MyRoute {
  static function get($slug, $name) {
   $app->get('/home/{name}', function ($name) use ($app) {
     return 'Home';
   });  
  }
}

如何使用if多个变量。

使用作为参数传递的匿名函数:

function get($slug, $action)
{
   $action();
}

有了参数,它就变成这样:

function get($slug, $action)
{
   $action( 42 );
}
get( 13 , function($x){echo $x;} );