Reputation: 295
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'
Upvotes: 1
Views: 1871
Reputation: 2561
//First of all you did not set the action="" of your form in postpage.php so make it correct give the full route path like action="http://localhost/slim/index.php/showpostdata" (like if you are using wamp server and you have index.php in slim folder and showpostdata is your post route identifier) now try the following code or copy it and try it..
//In postpage.php
<form action="http://localhost/slim/index.php/showpostdata" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit">
</form>
//In index.php which you will save in your slim folder
<?php
require 'vendor/autoload.php';
$app = new\Slim\Slim();
$app->post('/showpostdata', function () use ($app){
$us=json_decode($app->request()->getBody(), true);
//or
$us=$_POST;
//use one out of these above three or try all which gives you output..so In $us you will get an array of post data soo..
$unm=$us["username"];
$pwd=$us["password"];
//now you have your post form data user name and password in variable $unm and $pwd now echo it...
echo $unm."<br>";
echo $pwd;
});
$app->run();
?>
Upvotes: 0
Reputation: 18522
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.
Upvotes: 1