Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Using getHeader

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 698 Views
  • nekranox
    Avatar
    Community Member
    31 Posts

    Using getHeader Link to this post

    Hey guys,

    I've got a function which I would like to return $_SERVER['http_referer'] to the template.

    I've worked out that I need to use getHeader() but I can't seem to get my scope right to access the method.

    Here's my code (i've taken out non relevent bits):

    class HomePage extends Page{
       
       function ifReferrerSwitchImage() {
          return $this->owner->request->getHeader('Referer');
       }

    }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using getHeader Link to this post

    scope can be one of those things... if you change it from the root level with a <% control Something %> you need to use something like $Top.ifReferrerSwitchImage or <% control Top %>$ifReferrerSwitchImage<% end_control %>

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Using getHeader Link to this post

    Hey Swaiba,

    Thanks for your reply! Not quite understanding though sorry.

    Perhaps I didn't explain it well enough. The method that I can't access is getHeader. Using the example above I get the error: "Call to a member function getHeader() on a non-object".

    I guess it's my lack of knowledge, I just don't know where I am in the overall scope of silverstripe or how to travese the different levels of objects to get the getHeader method. Don't suppose you know where I can learn more about that?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using getHeader Link to this post

    "Call to a member function getHeader() on a non-object".
    means that "$this->owner->request" is a non object.

    Since I don't fully understand what is going on with that I'd advise setting debug mode and doing some "Debug::show($this)" and such around it. also if it is a non-object sometimes and others it is an object, test it first... e.g.

    if ($this->owner->request) return $this->owner->request->getHeader('Referer');

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Using getHeader Link to this post

    I've tried debugging and I'm not get anywhere

    Surely this is a really straight forward question? I'm inside a controller for a custom page type and I want to access the referer URL?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Using getHeader Link to this post

    Surely deugging is straightforward (http://doc.silverstripe.org/sapphire/en/topics/debugging)?

    I trusted that the bit you had "worked out" worked and you knew what you were doing and your issues was first in the template and then some other reason... maybe if you hand;t included code that obviously does not work - or indicated that it didn't work that might have helped.

    why don't you just return $_SERVER['http_referer'] itself?

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Using getHeader Link to this post

    Sorry, I didn't mean to suggest that you weren't helping. I was just frustrated because I wasn't getting anywhere. I appreciate that your trying your best to help

    When I use $_SERVER I get un-defined index, I wanted to reference it properly through the framework.

    I can see now when I re-read my original message that I'm suggesting that the problems in the template. I sohuld of been more clear.

    The code I provided doesn't work, I get non-object when trying to call getHeader and I'm not sure what object it belongs to or how get at it from within my function so that I can return it to the template.

    698 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.