21489 Posts in 5783 Topics by 2622 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2147 Views |
-
[SOLVED] - [Notice] Undefined variable: BaseHref

6 January 2010 at 6:56pm Last edited: 7 January 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?
-
Re: [SOLVED] - [Notice] Undefined variable: BaseHref

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.
-
Re: [SOLVED] - [Notice] Undefined variable: BaseHref

7 January 2010 at 1:51am Last edited: 7 January 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)
-
Re: [SOLVED] - [Notice] Undefined variable: BaseHref

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());
}
| 2147 Views | ||
|
Page:
1
|
Go to Top |


