[英]Slim 3 redirect route loop not working
我試圖做一個簡單的路由,以基於數組/鍵將端點重定向到特定的URL。
$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);
});
}
一旦進入$app->get
函數,就可以毫無問題地創建端點,這將不允許我使用$url
...在控制台中出現Undefined Index
錯誤。
我在這里做錯什么,為什么我不能訪問$url
變量?
要允許該函數從自身作用域之外訪問$url
,可以使用function() use() {
語法...
foreach($redirects as $endpoint => $url) {
$app->get($endpoint, function($request, $response) use ($url) {
return $response->withRedirect($url);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.