smile 22121 smile 22121 - 1 year ago 113
PHP Question

How to display HTTP POST data in SLIM 2 framework

In SLIM 2 I have a small form to display a username.

My index.php

$app = New \SlimController\Slim()

//Define routes
$app->addRoutes(array('/' => array('get' => 'Home:indexGet','post' => 'Home:indexPost'),
));


In postpage.php :

<form action="" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit">
</form>


Here is my Controller function :

public function indexPostAction()
{
$this->render('postpage');
}


And here is my postpage.html

var_dump($app->request->post("username"));


I get this error :


exception 'ErrorException' with message 'Undefined variable: app'


I tried also tried
var_dump($this->app->request->post("username"));

and I get


exception 'ErrorException' with message 'Undefined variable: this'

Answer Source

Slim's (v 2.x) View class doesn't store app reference. The "shortest" way to getting the app (and then the request object) from inside view (except using the static Slim:getInstance method) is

$this->data->get('flash')->getApplication()->request()->post();

but this just looks like a workaround and not an official way.

If your view requires data, it should get it through the render method:

$app->render('template', array('postdata' => $app->request()->post()));

If this is not sufficient for you, create a View-subclass that holds reference to the app and set it as the default view.