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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[SOLVED] - [Notice] Undefined variable: BaseHref


Reply

4 Posts   2841 Views

Avatar
marblegravy

6 January 2010 at 6:56pm (Last edited: 7 January 2010 1:52am), Community Member, 19 Posts

I'm trying to set up a function to return a short url of the current page.
In mySite/Code/Design.php:

class Design_Controller extends Page_Controller {

   function getTrimUrl() {
       $pathPartA = $BaseHref;
       $pathPartB = $URLSegment;
       $pathComplete = $pathPartA.$pathPartB;
       $tinyurl = file_get_contents("http://api.tr.im/api/trim_simple?url=$pathComplete");

       return $tinyurl;
   }
   
}

then in themes/mhytheme/templates/layout/Design.ss:

<a href="http://twitter.com/home?status=$TwitterText - <% if getTrimURL %>$getTrimURL<% else %>Donkeyballs<% end_if %>"><img src="/themes/mytheme/images/tweet.gif" alt="Tweet about this design!" /></a>

But I get errors from the controller that $BaseHref and $URLSegment are unknown variables. I thought they were supposed to be globally available?

Avatar
Willr

6 January 2010 at 10:16pm Forum Moderator, 5511 Posts

They aren't global vars, they are attributes though. If you want to call them from php then you can use Director::absoluteBaseURL(); to return the base url and for $URLSegment you need to use $this->URLSegment.

Avatar
marblegravy

7 January 2010 at 1:51am (Last edited: 7 January 2010 1:53am), Community Member, 19 Posts

Thanks Willr - fixed my problem perfectly. I just needed to know what the right syntax was.

For anyone else interested, I created a tweet link that lets people tweet about your page with content you specify.

In mySite/Code/[yourPage].php:

class YourPage_Controller extends Page_Controller {
function getTrimUrl() {
       $pathPartA = Director::absoluteBaseURL();
       $pathPartB = $this->URLSegment;
       $pathComplete = $pathPartA.$pathPartB;
       $tinyurl = file_get_contents("http://api.tr.im/api/trim_simple?url=".$pathComplete);
       return $tinyurl;
   }
}

In themes/[mytheme]/templates/layout/[yourPage].ss:

<a href="http://twitter.com/home?status=$TwitterText - <% if getTrimURL %>$getTrimURL<% else %>yourdefaulturl.com<% end_if %>">Tweet about this page!</a>

($TwitterText is a custom text field in [yourPage].ss)

Avatar
Willr

7 January 2010 at 8:54am Forum Moderator, 5511 Posts

Actually you could try to make that code even simpler. Since you're trying to get the full url I'm pretty sure using $this->Link() returns what you want. So try the following and see if this works.

function getTrimUrl() {
return file_get_contents("http://api.tr.im/api/trim_simple?url=".$this->Link());
}