Slim4 - getHeader 返回空

发布于 2025-01-17 18:06:26 字数 1900 浏览 0 评论 0原文

我从 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);
    });

Postman 请求调用

LOG 结果和空数组日志结果

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);
    });

Postman request call

LOG Results and empty array:
LOG Result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2025-01-24 18:06:26

您需要使用$ request-&gt; getheader('userId')[0]

you need to use $request->getHeader('userId')[0]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文