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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] - [Notice] Undefined variable: BaseHref


Go to End


4 Posts   4516 Views

Avatar
marblegravy

Community Member, 19 Posts

6 January 2010 at 6:56pm

Edited: 07/01/2010 1:52am

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

Forum Moderator, 5523 Posts

6 January 2010 at 10:16pm

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

Community Member, 19 Posts

7 January 2010 at 1:51am

Edited: 07/01/2010 1:53am

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

Forum Moderator, 5523 Posts

7 January 2010 at 8:54am

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