douche3791 2014-09-02 10:58
浏览 46
已采纳

无法从PHP SLIM框架访问ember-data REST请求标头

This is the RESTAdapter code as in the Ember js guide:

App.ApplicationAdapter = DS.RESTAdapter.extend({
   namespace: 'emberboot/api',

   headers: {
      'API_KEY': 'secret key'
  }
});

This is the code for getting headers from SLIM PHP. The get headers function is documented here http://docs.slimframework.com/#Request-Headers

// - http://localhost/emberboot/api/posts
// - $app->get('/posts', 'getPosts'); //get all post

function getPosts() 
{
    $request = \Slim\Slim::getInstance()->request();
    $key = $request->headers->get('API_KEY');

    //logging
    $file = 'headers.txt';
    file_put_contents($file, $key);
    ;
    ;

This is from the Chrome Developer Tool:

enter image description here

Clearly there is nothing wrong from the Ember-data part coz the header is there. However THE LOGGING FILE: headers.txt contain no text at all - empty!

UDATED - ANSWERS AS SOLVE BY @engvrdr. Use dash instead of underscore

This is the RESTAdapter code UPDATED!

App.ApplicationAdapter = DS.RESTAdapter.extend({
   namespace: 'emberboot/api',

   headers: {
      'API-KEY': 'secret key'
  }
});

The Slim PHP code updated:

function getPosts() 
{
    $request = \Slim\Slim::getInstance()->request();
    $headers = $request->headers;
    $apiKey = $headers->get('API-KEY');
    $file = 'headers.txt';
    file_put_contents($file, $apiKey);
    ;
    ;
  • 写回答

1条回答 默认 最新

  • duanqin7791 2014-09-02 11:07
    关注

    I'm not much a slim expert but can you try? (In documentation request seems like a property not a method)

    function getPosts() 
    {
    $app = \Slim\Slim::getInstance();
    $key = $app->request->headers->get('API-KEY');
    
    //logging
    $file = 'headers.txt';
    file_put_contents($file, $key);
    

    Edit : As i found out it is an issue about underscores (_) in HTTP header names (Why underscores are forbidden in HTTP header names)

    if you change it to API-KEY it will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站