2013-05-08 95 views
1

过去几天我一直在玩Silex。我无法理解下面的代码片段如何工作。PHP Silex微框架语法解释

我对不感兴趣什么它确实,而是如何它做到了。

use Silex\Application; 
    use Symfony\Component\HttpFoundation\Request; 
    use Symfony\Component\HttpKernel\HttpKernelInterface; 

    $app->get('/foo', function (Application $app, Request $request) { 
     $subRequest = Request::create('/', ...); 
     $response = $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); 

     return $response; 
    }); 

我弄糊涂各地

function (Application $app, Request $request) 

从我的理解这是一个匿名函数被调用作为参数传递给$ APP-> get方法。这个匿名函数中的两个参数是如何工作的?具体是什么:

Application $app, Request $request

谢谢!

+0

你认为它有什么作用?你有没有看过以前看起来像这样的东西?也许在类方法或其他功能? – ircmaxell 2013-05-08 20:10:17

+0

我有我的怀疑,但不知道是否可以在OOP中使用typehinting。 – Carvefx 2013-05-09 20:43:53

回答

9

控制器参数填充的隐含方式可能是silex中最令人困惑的部分之一。

当您通过某个HTTP动词方法定义控制器时,该控制器可以向其参数添加类型提示。 RequestApplication是两个参数$app$request的类型提示。

HttpKernel是负责调用控制器的Silex的一部分。它使用ControllerResolver来确定要传入哪些参数。ControllerResolver将从类型提示中推断参数。

  • 对于Silex\Application类型的提示,它将注入应用程序。
  • 对于Symfony\Component\HttpFoundation\Request类型提示,它将注入当前请求。

如果您想更好地了解,我建议您阅读the HttpKernel source code

+0

你的回答有点深入那么我在找什么,我只是对语法感兴趣:)这个:http://php.net/manual/en/language.oop5.typehinting.php是我的寻找。谢谢! – Carvefx 2013-05-09 20:43:01