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条回答 默认 最新

相关推荐 更多相似问题