Search code examples
phproutesfat-free-framework

Hand over "data/params" on reroute(); in "fat free framework"


Im looking for an elegant way to hand over data/params when using $f3->reroute();

I have multiple routes configured in a routes.ini:

GET @sso: /sso/first [sync]  =   Controller\Ccp\Sso->first, 0
GET @map: /map       [sync]  =   Controller\MapController->second, 3600

Now I reroute(); to @map route, from first();

class Sso {
    public function first($f3){
        $msg = 'My message!';

        if( !empty($msg) ){
            $f3->reroute('@map');
        }
    } 
}

Is there any "elegant" way to pass data (e.g. $msg) right into $MapController->second(); ?

I don´t want to use $SESSION or the global $f->set('msg', $msg); for this.


Solution

  • This isn't an issue specific to fat-free-framework, but web in general. When you reroute, you tell the browser to redirect the user's browser page using a 303 header redirect code. Take a minute to read the doc regarding re-routing: http://fatfreeframework.com/routing-engine#rerouting

    There seems to be some contradicting information in your question, which leads me to question the purpose of what you are trying to achieve.

    If you are rerouting, you can either use the session, cookies, or use part of the url to pass messages or references to a message.

    If you do not need to redirect, but just want to call the function without changing the passed parameters, you could abstract the content of the function and call that function from both routes. You could also use the $f3 globals, which are a great way of passing data between functions in cases where you don't want to pass the data using the function call. is there a reason why you don't want to to use this? The data is global for the single session, so there is no security concern, and the data gets wiped at the end of the request, so there is very little extra footprint or effect on the server.