如何使用slim获取post数据。我有一个像这样的函数调用
$this->app->post("/post/create", array($this, "createPost"));
我想从PHP HTML表单获得的帖子数据。我接受了这样的请求
$request = 'Slim'Slim::getInstance()->request();
并获取如下的post数据
$userId = $_POST["user"];
$content = $_POST["content"];
$datetime = $_POST["date"];
$filename = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$filetmpname = $_FILES["image"]["tmp_name"];
这是正确的做法吗?
如果您使用Slim你可以这样做:
$app->post('/users', function ($request, $response) {
$formDataArry = $request->getParsedBody();
// do something with formDataArry
});
你可以这样做:
$this->app = new 'Slim'Slim();
$this->app->post("/post/create", function () {
$userId = $this->app->request->post('user');
// or
$allPostVars = $this->app->request->post();
$userId = $allPostVars['user'];
//...
});
如果你不想使用匿名函数("在PHP 5.4.0之前不可能使用$this from anonymous function "),我认为你可以这样做:
$this->app->post("/post/create", 'createPost');
function createPost() {
$userId = $this->app->request->post('user');
//...
}
如果你使用Slim 3:
<?php
namespace Vendor'Product'Classes;
use Psr'Http'Message'ServerRequestInterface as Request;
use Psr'Http'Message'ResponseInterface as Response;
class LoginController
{
public function loginUser(Request $request, Response $response)
{
$username = $request->getParam('username');
$password = $request->getParam('password');
// Logic to validate login ommited.
}
}