1

I am using the php flight framework: http://flightphp.com/ I want to call flight::route(); a few time to automatically create a few routers. But i need a variable in the callback function.

I'm trying:

foreach($pages as $page) {
    Flight::route("/" . $page['route'], function() {
        // I need $page here
    });
}


Flight::start();

Where $pages is an array of objects I created. And I need those objects when the route is triggered. How do I do this?

Maybe somebody can help me with this. The Flightphp framework has a method Flight::set("variable", value) and Flight::get("variable");

Maybe I could do:

foreach($pages as $page) {
    Flight::route("/" . $page['route'], function() {
        $page = Flight::get("page");
        var_dump($page);
        // I need $page here
    });
    Flight::set("page", $page);

}

But this will make me always have the LAST page. While I might like to route to the first or any other page.

DenniZr
  • 43
  • 6
  • have you tried `function($page)`? – Dave Aug 10 '15 at 12:04
  • Yes the function is a callback function and later called by the flight framework. So $page is never passed. I don't call the function. When I do as you say I get: Missing argument 1 for {closure}(), called in ../flight/core/Dispatcher.php on line 160 and defined (2) – DenniZr Aug 10 '15 at 12:07
  • Seems relevant: [Callback function using variables calculated outside of it](https://stackoverflow.com/q/4588714/2943403) – mickmackusa Mar 25 '23 at 06:24

2 Answers2

0

I finally did:

Flight::set("pages", $pages);
Flight::route("/@route", function($route) {
    $pages = Flight::get("pages");
    foreach($pages as $page) {
        if($page['route'] == $route) {
            include_once $page['controller'];
            if(isset($activepage))$activepage->draw();
        }
    }
});
DenniZr
  • 43
  • 6
0

Looking at the framework docs (never used it before) it looks like you can do named params in to route where you pull the params from the url eg: /mypage/var1/var2 etc in your case you'd have to loop through your array and generate the flight::route() bits against some form of pattern rather than as you seem to be doing it which is identifying the page name and attempting to set a route based off that.

So your code would be something like this I think

foreach($pages as $page) {
    Flight::route('/@'.$page, function($localpage){
        echo "I am, $localpage";
    });
}

Failing that it appears it will also give you a result of all params passed in to route so you could perhaps filter of there something like this though you'd have to then filter the params inside the route. (move your foreach inside the callback)

Flight::route('/', function($route){
    // Array of named parameters
    $route->params;
}, true);
Dave
  • 3,280
  • 2
  • 22
  • 40
  • Thanks Dave. I will put my foreach inside the route and compare there, for my page object holds more than just page names. And i need that when there is a match. – DenniZr Aug 10 '15 at 21:35
  • where possible you're still better off registering each at application instance rather than attempting to build them on the fly it'll work out a little bit quicker and separates your application logic (generating the routes) from your page logic (function of the route) – Dave Aug 10 '15 at 21:38