smile 22121 smile 22121 - 1 year ago 99
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">

Here is my Controller function :

public function indexPostAction()

And here is my postpage.html


I get this error :

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

I tried also tried

and I get

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


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


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.