How do you send POST parameters to a function in PHP using Fat-Free-Framework?

2.6k views Asked by At

The documentation on the website is a bit lacking (http://fatfreeframework.com/routing-engine). I want to use the shorthand expression of POST:

$f3->route('POST /login','Auth::login');

How do you send params into the Auth->login() function above?

This is an alternative way of writing it, but a bit longer:

$f3->route('POST /login',
    function($f3) {
        $params = $f3->get('POST');
        $Auth = new Auth;
        $Auth->login($params['username'], $params['password']);
    }
);
2

There are 2 answers

2
xfra35 On BEST ANSWER

If you mean that the Auth::login should automatically receive the POST data as an argument, then you can't.

All F3 route handlers receive the following arguments:

  1. the framework instance
  2. the route tokens (if any)

See here for an example.

Anyway, if the Auth->login function you're referring to is the one included in the framework, then it couldn't work in any manner since the login() function is not a route handler. It simply returns TRUE or FALSE. A route handler has to do a little bit more than that: for example, to reroute the user on success or to display the login form again on error.

0
Forien On

Try:

$f3->route('POST /login',
function($f3,$params) {
    $Auth = new Auth;
    $Auth->login($params['username'], $params['password']);
}
);