使用Slim Framework具有相同匿名回调的多个路由

Fra*_*isc 12 php slim

如何定义使用相同匿名回调的多个路由?

$app->get('/first_route',function()
{
   //Do stuff
});
$app->get('/second_route',function()
{
   //Do same stuff
});
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用一个可以工作的函数的引用,但我更喜欢使用匿名函数与代码库的其余部分保持一致的解决方案.

所以基本上,我正在寻找的是一种做这样的事情的方式:

$app->get(['/first_route','/second_route'],function()
{
       //Do same stuff for both routes
});
Run Code Online (Sandbox Code Playgroud)

〜或〜

$app->get('/first_route',function() use($app)
{
   $app->get('/second_route');//Without redirect
});
Run Code Online (Sandbox Code Playgroud)

谢谢.

小智 19

您可以使用条件来实现这一目标.我们使用它来翻译URL.

$app->get('/:route',function()
{
    //Do same stuff for both routes
})->conditions(array("route" => "(first_route|second_route)"));
Run Code Online (Sandbox Code Playgroud)


Eug*_*ene 13

我不能给你一个框架特定的解决方案,但如果它有帮助你可以引用匿名功能:

$app->get('/first_route', $ref = function()
{
   //Do stuff
});
$app->get('/second_route', $ref);
Run Code Online (Sandbox Code Playgroud)