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

Handle post request like get request

Thomas K.'s Avatar

Thomas K.

15 Mar, 2012 10:01 PM

Hello,

Is it possible to handle post request like get request ?
For example :

` <?php
require 'Slim/Slim.php';

$app = new Slim();

$app->get('/', function () {

$app = Slim::getInstance(); $app->render('view.phtml'); });

$app->post('/hello/:name', function ($name) { echo 'Bonjour '.$name; })->name('hello');

$app->run(); `

This example doesn't work but maybe there's another solution.

Thank you.
BR.

  1. 1 Posted by Garnier on 16 Mar, 2012 12:20 PM

    Garnier's Avatar

    I don't see what is the problem.

    If you are using a form to post the data the action attribute should match your route pattern, in this case:

    <form method="post" action="/hello/Peter"><input type="submit"/></form>
    

    In the other hand, if what you want is use the same route for POST and GET request you can do this:

    $app->map('/hello/:name', function ($name) { echo 'Bonjour '.$name; })->name('hello')->via('GET', 'POST');
    
  2. Support Staff 2 Posted by Andrew Smith on 19 Mar, 2012 11:16 AM

    Andrew Smith's Avatar

    A better option for the form would be to use the urlFor function.

    <form method="post" action="<?php echo $app->urlFor('hello', array('name' => 'Peter')); ?>"><input type="submit"/></form>
    

    Take a look at the articles on my website which might be helpful Slim and PHPActiveRecord and Slim and Twig

  3. 3 Posted by Thomas K. on 19 Mar, 2012 03:02 PM

    Thomas K.'s Avatar

    No, the result isn't good.

    This is my html form :

    <form method="post" action="<?php echo Slim::getInstance()->urlFor('hello') ?>"> <input type="text" name="name" /> <input type="submit" value="Say Hello" /> </form>

    And I want to handle the POST request as a GET request :

    $app->map('/hello/:name', function ($name) { echo 'Salut '.$name; })->name('hello')->via('GET', 'POST');

    Is it possible ?

  4. Support Staff 4 Posted by Andrew Smith on 19 Mar, 2012 03:13 PM

    Andrew Smith's Avatar

    Your code is not formatted but I think I can work it out anyway. Your current code wouldn't work since :name is not optional so on post that shouldn't work, in order to get a post variable you will need to use

    $_POST
    

    or

    $app->request()->post('name');
    

    In order to make :name optional wrap it in () so your code should look like this

    $app->map('/hello(/:name)', function ($name = '') {
        switch ($app->request()->getMethod()) {
            case "GET":
                echo 'Salut '.$name;
            break;
            case "POST":
                echo $app->request()->post('name');
            break;
        }
    })->name('hello')->via('GET', 'POST');
    

    Take a look at optional routes here http://www.slimframework.com/documentation/develop#optionalroutepar...

  5. Andrew Smith closed this discussion on 10 Jul, 2012 12:28 PM.

Comments are currently closed for this discussion. You can start a new one.

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