Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] - [Notice] Undefined variable: BaseHref

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

Page: 1
Go to End
Author Topic: 2763 Views
  • marblegravy
    Avatar
    Community Member
    19 Posts

    [SOLVED] - [Notice] Undefined variable: BaseHref Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: [SOLVED] - [Notice] Undefined variable: BaseHref Link to this post

    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.

  • marblegravy
    Avatar
    Community Member
    19 Posts

    Re: [SOLVED] - [Notice] Undefined variable: BaseHref Link to this post

    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)

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: [SOLVED] - [Notice] Undefined variable: BaseHref Link to this post

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

    2763 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.