doxp30826 2018-06-09 06:53 采纳率: 0%
浏览 40
已采纳

Slim 3重定向路由循环不起作用

I'm trying to make a simple route to redirect endpoints to a specific URL based on an array/key.

$redirects = [
    "/ios" => $GLOBALS['config']['iosAppStoreLink'],
    "/android" => $GLOBALS['config']['androidAppStoreLink']
];

/**
 * Redirects
 */
foreach($redirects as $endpoint => $url) {
    $app->get($endpoint, function($request, $response) {
        return $response->withRedirect($url);
    });
}

The endpoints get created without issue just once I'm inside of the $app->get function, it will not allow me to use $url... I get an Undefined Index error in my console.

What am I doing wrong here, why am I not able to access the $url variable?

  • 写回答

1条回答 默认 最新

  • doumeng3345 2018-06-09 07:06
    关注

    To allow the function to access the $url from outside of it's own scope, you could use function() use() { syntax...

    foreach($redirects as $endpoint => $url) {
        $app->get($endpoint, function($request, $response) use ($url) {
            return $response->withRedirect($url);
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料