Slim 3 - 替代 isPost()?

标签 slim php-5.6 slim-3 psr-7

在 Slim 2 中,我会这样做,

$app->map('/login', function () use ($app) {

    // Test for Post & make a cheap security check, to get avoid from bots
    if ($app->request()->isPost() && sizeof($app->request()->post()) >= 2) {

        //
    }

    // render login
    $app->render('login.twig');

})->via('GET','POST')->setName('login');

但是在 Slim 3 中,

// Post the login form.
$app->post('/login', function (Request $request, Response $response, array $args) {

    // Get all post parameters:
    $allPostPutVars = $request->getParsedBody();

    // Test for Post & make a cheap security check, to get avoid from bots
    if ($request()->isPost() && sizeof($allPostPutVars) >= 2) {

        ///
    }

});

我收到此错误,

Fatal error: Function name must be a string in C:...

显然 isPost() 已被弃用,那么在 Slim 3 中我应该使用什么来替代 isPost 呢?

最佳答案

在 Slim 4 中,没有这样的帮助器,因此语法变得更长(就像很多 Slim 4 的东西一样):

$request->getMethod() === 'POST'

关于Slim 3 - 替代 isPost()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330573/

相关文章:

Android Retrofit - POST 请求不起作用,但在 Postman 中它有效

php - 使用 Twig 模板引擎为具有所需占位符 (/post/{id} ) 的路由动态创建链接

php - slim : how to log all request to the framework

php - 迁移到 PHP 5.6 : Writing a MySQL wrapper

php - 在 Slim v3 中全局设置模板数据

php - 使用 Symfony 2 Finder 组件获取子目录

php - 为什么 suhosin.executor.disable_emodifier 不起作用?

php - 使用curl发生API错误时如何读取响应体?

php - Slim 框架 - 创建一条返回可下载文件流的路由

phpunit - slim3 中的 Controller 单元测试