Slim4 - getHeader 返回空
我从 slim4 $request->getHeader('accept')
方法获取一个空数组来进行校验和授权。另外 $request->getParsedBody()
工作正常。我按照如下文档中的代码进行操作:
<?php
declare(strict_types=1);
namespace App\Application\Actions\Users;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Slim\Psr7\Response;
use Cake\Validation\Validator;
use Selective\Validation\ValidationResult;
use Selective\Validation\Factory\CakeValidationFactory;
use Selective\Validation\Exception\ValidationException;
use PDO;
use \Firebase\JWT\JWT;
use App\Application\Helpers\CommonHelper;
class ChangePwdAction implements RequestHandlerInterface
{
private $logger;
private $connection;
public function __construct(PDO $connection,LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
echo $request->getHeader('userId');exit;
}
}
API 调用:
<?php
declare(strict_types=1);
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Application\Actions\Users\ListUserAction;
use App\Application\Actions\Users\ChangePwdAction;
$app->group('/v1/user', function (Group $group) {
$group->get('', ListUserAction::class);
$group->post('/changePwd/{id}', ChangePwdAction::class);
});
I am getting an empty array from slim4 $request->getHeader('accept')
method to do checksum authorization. Also $request->getParsedBody()
is working fine. I followed the code from documentation like below:
<?php
declare(strict_types=1);
namespace App\Application\Actions\Users;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Slim\Psr7\Response;
use Cake\Validation\Validator;
use Selective\Validation\ValidationResult;
use Selective\Validation\Factory\CakeValidationFactory;
use Selective\Validation\Exception\ValidationException;
use PDO;
use \Firebase\JWT\JWT;
use App\Application\Helpers\CommonHelper;
class ChangePwdAction implements RequestHandlerInterface
{
private $logger;
private $connection;
public function __construct(PDO $connection,LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
echo $request->getHeader('userId');exit;
}
}
API Call:
<?php
declare(strict_types=1);
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Application\Actions\Users\ListUserAction;
use App\Application\Actions\Users\ChangePwdAction;
$app->group('/v1/user', function (Group $group) {
$group->get('', ListUserAction::class);
$group->post('/changePwd/{id}', ChangePwdAction::class);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用$ request-&gt; getheader('userId')[0]
you need to use $request->getHeader('userId')[0]