如何定义使用同一匿名回调的多个路由?
$app->get('/first_route',function()
{
//Do stuff
});
$app->get('/second_route',function()
{
//Do same stuff
});
我知道我可以使用对函数的引用,但我更喜欢使用匿名函数的解决方案与代码库的其他部分保持一致。
所以基本上,我想要的是一种做这样事情的方法:
$app->get(['/first_route','/second_route'],function()
{
//Do same stuff for both routes
});
~或~
$app->get('/first_route',function() use($app)
{
$app->get('/second_route');//Without redirect
});
谢谢。
您可以使用条件来实现这一点。我们用它来翻译URL。
$app->get('/:route',function()
{
//Do same stuff for both routes
})->conditions(array("route" => "(first_route|second_route)"));
我不能给你一个特定于框架的解决方案,但如果它有帮助,你可以引用匿名函数:
$app->get('/first_route', $ref = function()
{
//Do stuff
});
$app->get('/second_route', $ref);
回调是委托。所以你可以这样做:
$app->get('/first_route', myCallBack);
$app->get('/second_route', myCallBack);
function myCallBack() {
//Do stuff
}