The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

can't get post parametres

ok.abdo's Avatar

ok.abdo

14 Dec, 2015 04:21 PM

I'm trying to get post parameters inside the router callback function :

$app->post('/login', function ($request, $response, $args) {
    var_dump($request->post('username'));
    die();
});
but it gives me this error , Normally it's supposed to work :
Fatal error:  Call to undefined method Slim\Http\Request::post() in C:\xampp\htdocs\xxxs\index.php on line 21
  1. 1 Posted by Rob Allen on 14 Dec, 2015 04:23 PM

    Rob Allen's Avatar

    in Slim3, you want $request->getParsedBody()['username'] or $request->getParam('username')

  2. 2 Posted by ok.abdo on 14 Dec, 2015 04:25 PM

    ok.abdo's Avatar

    @Allen Thank you I didn't notice that ...last time I worked with slim was like 2 years ago :D

  3. 3 Posted by Tony on 05 Jan, 2016 11:51 AM

    Tony's Avatar

    i gave your suggestion above a try...

    $app->post('/contact', function($request, $response, $args){
        $name = $request->getParam('name'); // by key
        $email = $request->getParam('email'); // by key
        $msg = $request->getParam('msg'); // by key
        //$data = $request->getParams(); // all data from query string or post body

        return $this->view->render($response, 'contact.twig');

    });
    but slim3 comes up with following error...

    Method not allowed. Must be one of: POST

    Is this the correct post syntax for slim3?

  4. 4 Posted by slimguy on 05 Jan, 2016 02:05 PM

    slimguy's Avatar

    That error will show if you're sending a GET request to the server, and you don't have a GET route for '/contact'

    You can only post to that current route.

  5. 5 Posted by Filipe on 05 Jan, 2016 11:16 PM

    Filipe's Avatar

    Try to get POST parameter like this:

    $app->post('/admin', function ($request, $response, $args) {
    $message = $request->getParsedBody()['text'];

    $response->write($message);
    return $response;

    });

  6. 6 Posted by Tony on 06 Jan, 2016 06:25 AM

    Tony's Avatar

    slimguy

    I don't get an error when i use the get http method. So get on the form & get as my route. When i do this, the page renders(using twig too) & i get my query string in thr Uri .

    Example
    /contact?fname=john+smith&email=john.smith%40gmail.com&msg=Example

    Only the post method. Slim not recognizing that its a POST request. I have post on the form & post in my route.

    Filipe,
    i tried that but same result.

    $app->post('/contact', function($request, $response, $args){
    $fullname = $request->getParsedBody()['fname'];
    $email = $request->getParsedBody()['email'];
    $msg = $request->getParsedBody()['msg'];
    $response->write($fullname);
    $response->write($email);
    $response->write($msg);

        return $this->view->render($response, 'contact.twig');
    })->setName('contact');

    i don't know what's going on...

    any other suggestions?

    Tony

  7. 7 Posted by Tony on 06 Jan, 2016 09:09 AM

    Tony's Avatar

    Finally a solution...

    two routes need to be created $app->get() & $app->post()

    the reason is that by executing the URL in the browser, this becomes a GET request.

    I var_dumped just to be sure the post form data values were passed using SUBMIT & they did. And when jumping to & from the /contact page , i echo out a string to verify when the get route is used.

    // CONTACT PAGE $app->post('/contact', function($request, $response, $args){ $fname = $request->getParam('fname'); $email = $request->getParam('email'); $msg = $request->getParam('msg');

    var_dump($fname);
    var_dump($email);
    var_dump($msg);

    return $this->view->render($response, 'contact.twig');
    })->setName('contact'); // added route name

    // CONTACT PAGE $app->get('/contact', function($request, $response, $args){ echo "from side get route"; return $this->view->render($response, 'contact.twig'); })->setName('contact'); // added route name

    Tony

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac