2

In Slim 2, I would do this,

$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');

But in 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) {

        ///
    }

});

I get this error,

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

Obviously that isPost() is deprecated, so what should I use instead in Slim 3 for isPost's replacement?

alexw
  • 8,468
  • 6
  • 54
  • 86
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

2

In Slim 4, there's no such helper and so the syntax gets longer (like a lot of Slim 4 stuff):

$request->getMethod() === 'POST'
Fabien Snauwaert
  • 4,995
  • 5
  • 52
  • 70
1

According to documentation and comments, Slim supports these proprietary methods:

  • $request->isGet()
  • $request->isPost()
  • $request->isPut()
  • $request->isDelete()
  • $request->isHead()
  • $request->isPatch()
  • $request->isOptions()

Here it is an example of usage:

<?php
require 'vendor/autoload.php';

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

$app = new \Slim\App;
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS'], '/', function (ServerRequestInterface $request, ResponseInterface $response) {
    echo "isGet():" . $request->isGet() . "<br/>";
    echo "isPost():" . $request->isPost() . "<br/>";
    echo "isPut():" . $request->isPut() . "<br/>";
    echo "isDelete():" . $request->isDelete() . "<br/>";
    echo "isHead():" . $request->isHead() . "<br/>";
    echo "isPatch():" . $request->isPatch() . "<br/>";
    echo "isOptions():" . $request->isOptions() . "<br/>";

    return $response;
});

$app->run();
Community
  • 1
  • 1
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53