php - Slim framework get the data from a form -
i try make simple form slim framework. don't know how display posted data. want try echo it. heard need use library respect, think slim can such small thing.
here code :
require '../../vendor/slim/slim/slim/slim.php'; \slim\slim::registerautoloader(); $app = new \slim\slim(); $app->get('/', function() use ($app){ $app->render('form.php'); }); $app->post('/', function() use ($app){ $req = $app->request(); $errors = array(); $params = array( 'email' => array( 'name'=>'email', 'required'=>true, 'max_length'=>64, ), 'subject' => array( 'name'=>'subject', 'required'=>true, 'max_length'=>256, ), ); //submit_to_db($email, $subject, $message); $app->flash('message','form submitted!'); $app->redirect('./'); }); $app->run();
in slim 2, can access posted data using post()
method of request()
:
$app->post('/', function () use ($app) { $request = $app->request(); $email = $request->post('email'); $subject = $request->post('subject'); echo "email: $email<br/>"; echo "subject: $subject"; });
in slim 3, request passed callback/controller/etc. , can call getparam()
fetches value body or query string (in order):
$app->post('/', function ($request, $response, $args) { $email = $request->getparam('email'); $subject = $request->getparam('subject'); echo "email: $email<br/>"; echo "subject: $subject"; });
starting slim 3.1 there getparsedbodyparam()
, getfetchparam()
fetch body or query string. (pr #1620)
reminder, can provide default value: $request->getparam('email', 'default_value')
Comments
Post a Comment