【发布时间】:2017-04-04 11:21:26
【问题描述】:
我正在使用 CakePHP 3 框架开发一个 API。现在我从 POSTMAN 客户端发送一个 GET 请求。用户将在标头中传递 API 密钥。
我想在我的控制器函数中获取这个标头。
这就是我的控制器的样子
namespace Api\Controller;
use Cake\Auth\DefaultPasswordHasher;
use Api\Controller\AppController;
use Cake\Cache\Cache;
use Cake\Http\ServerRequest;
class ApiController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function myinfo()
{
if($this->request->is('get')) {
$key = $this->request->getHeaderLine('Authorization');
$this->set('key', $key);
}
$this->set('_serialize', ['key']);
}
}
我得到的错误是:HeaderLine is not a function
我还尝试了更多选项:
$acceptHeader = $this->request->getHeader('Authorization');
但这也引发了类似的错误。标头不是函数。
参考:Link
CakePHP 版本:3.3.5
【问题讨论】:
-
请务必提及您的 准确 CakePHP 版本(
vendor/cakephp/cakephp/VERSION.txt的最后一行) - 谢谢! -
Cakephp 3.3.5 @ndm
-
那么您可能需要仔细查看链接文档中的最后一个示例。
标签: php cakephp cakephp-3.0