使用Slim Framework具有相同匿名回调的多个路由 [英] Multiple routes with the same anonymous callback using Slim Framework

查看:80
本文介绍了使用Slim Framework具有相同匿名回调的多个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I define multiple routes that use the same anonymous callback?

$app->get('/first_route',function()
{
   //Do stuff
});
$app->get('/second_route',function()
{
   //Do same stuff
});

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

I know I can use a reference to a function which would work, but I'd prefer a solution for using the anonymous function to be consistent with the rest of the codebase.

因此,基本上,我正在寻找一种执行以下操作的方法:

So basically, what I'm looking for is a way of doing something like this:

$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.

You can use conditions to achieve just that. We use that to translate URLs.

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

这篇关于使用Slim Framework具有相同匿名回调的多个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆