Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Using getHeader


Reply


7 Posts   918 Views

Avatar
nekranox

Community Member, 31 Posts

7 September 2011 at 2:50am

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

}

Avatar
swaiba

Forum Moderator, 1799 Posts

7 September 2011 at 2:58am

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 %>

Avatar
nekranox

Community Member, 31 Posts

7 September 2011 at 3:15am

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?

Avatar
swaiba

Forum Moderator, 1799 Posts

7 September 2011 at 3:57am

"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');

Avatar
nekranox

Community Member, 31 Posts

7 September 2011 at 10:39pm

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?

Avatar
swaiba

Forum Moderator, 1799 Posts

8 September 2011 at 12:45am

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?

Avatar
nekranox

Community Member, 31 Posts

8 September 2011 at 2:15am

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.