Slim 3 - isPost() 的替代品?
Slim 3 - replacement for isPost()?
在 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?
根据documentation and ,Slim 支持这些专有方法:
- $request->isGet()
- $request->isPost()
- $request->isPut()
- $request->isDelete()
- $request->isHead()
- $request->isPatch()
- $request->isOptions()
这是一个用法示例:
<?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();
在 Slim 4 中,没有这样的帮助器,因此语法变得更长(就像很多 Slim 4 的东西):
$request->getMethod() === 'POST'
在 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?
根据documentation and
- $request->isGet()
- $request->isPost()
- $request->isPut()
- $request->isDelete()
- $request->isHead()
- $request->isPatch()
- $request->isOptions()
这是一个用法示例:
<?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();
在 Slim 4 中,没有这样的帮助器,因此语法变得更长(就像很多 Slim 4 的东西):
$request->getMethod() === 'POST'