# Passing variable to route

**URL:** <https://forum.getkirby.com/t/passing-variable-to-route/35626>\
**Category:** Questions\
**Tags:** v5\
**Created:** [October 2, 2026, 12:51pm UTC](https://forum.getkirby.com/t/passing-variable-to-route/35626 "2026-10-02T12:51:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilsmielke](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nilsmielke/32/12501_2.png) [@nilsmielke](https://forum.getkirby.com/u/nilsmielke)\
**Post date:** [October 2, 2026, 12:51pm UTC](https://forum.getkirby.com/t/passing-variable-to-route/35626/1 "2026-10-02T12:51:16Z")

</div>

I have a plethora of redirects I would like to generate somewhat dynamically.  
My idea is to store them in an array like

```auto
$pagesFromOldSite = [
    [
        'pattern' => ['company-partners', 'partners', 'product/graph-databases'],
        'target' => 'about/ecosystem/partners',
    ],
    [
        'pattern' => ['publication', 'publications', 'research/publications'],
        'target' => 'about/ecosystem/publications',
    ],
];

```

—as that is more readable IMO—and then loop through them like this:

```auto
foreach ($pagesFromOldSite as $route):
    $routes[] = [
        'pattern' => $route['pattern'],
        'action' => function() {
            $page = page($route['target']);
            if(!$page) $this->next();
            go($page, 301);
        }
    ];
endforeach;

```

where `$routes` is the array of all routes, of course.  
This doesn’t work, obviously.  
But can I pass `$route['target']` as a variable to the `action` and if so, how?  
I have tried `'action' => function($target = $route['target']) {`, but to non avail.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [October 2, 2026, 2:54pm UTC](https://forum.getkirby.com/t/passing-variable-to-route/35626/2 "2026-10-02T14:54:23Z")

</div>

Not sure how PHP binds its variables exactly here but what happens when you do

```php
'action' => function() use ($route) {

```

Does it pass always the correct `$route` for each loop iteration or to all actions the very last loop iteration?

---

<div class="post-metadata">

**Author:** ![nilsmielke](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nilsmielke/32/12501_2.png) [@nilsmielke](https://forum.getkirby.com/u/nilsmielke)\
**Post date:** [October 2, 2026, 3:13pm UTC](https://forum.getkirby.com/t/passing-variable-to-route/35626/3 "2026-10-02T15:13:35Z")

</div>

That’s perfect, Nico!  
I thought, I had tried that, thinking that it failed due to it passing the last loops `$route`, but I must have had something wrong.  
Works perfectly now.  
Many thanks!
