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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

Converting a URLSegment to a Full-Path Link

Go to End

3 Posts   2476 Views


Community Member, 245 Posts

10 December 2010 at 6:10am


I just upgraded to 2.4.3 from 2.3.* and, as we know, with the advent of hierarchical URLs, URLSegment can no longer be used as a link since the nesting is not stored in that field. I realize that this was considered bad practice in the first place, but perhaps had I known how to convert a URLSegment TO a Link() in the first place then I wouldn't have done it ;)

That said, let's get to it. I use a lot of custom queries on my site using DB::query() and SQLQuery(). In these queries, the Link() function is meaningless. So I am getting the URLSegment from the SiteTree table and then just using that value as the Link. Now, as stated above, this doesn't work, because these pages are now nested within other parent pages.

I need to know how I can "calculate" the FULL URL from this URLSegment, much like the redirects performed by the MigrateSiteTreeLinkingTask action does. There has to be a method for this, but I haven't found it yet. Any help out there?

Thanks in advance,


Forum Moderator, 5523 Posts

10 December 2010 at 12:09pm

Well like you said you should be using $Link in templates etc. It looks like SiteTree's RelativeLink() function handles making the full link. You will still need to get the actual object record to call that though. AFAIK there doesn't appear to be a static function to 'convert' foo/ to /about-us/foo without orignally having the foo page object (i.e dataobject::get()) to start with.

The MigrateSiteTreeLinking doesn't actually rewrite the URL, it saves the ID then on output it renders the Link() function of the page.


Community Member, 245 Posts

11 December 2010 at 5:39am

Thanks for your reply @willr. How do you recommend getting the object record from the URLSegment?? Or, the ID. I can get the ID in my query, obviously. I guess you're saying use DataObject::get_by_id() and then pass that result to RelativeLink()?