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

Dynamically create redirector page


Reply

5 Posts   903 Views

Avatar
DeklinKelly

6 January 2011 at 4:18pm Community Member, 197 Posts

The following code creates a new page:

$p = new Page();
$p->Title = 'Hello';
$p->MenuTitle = $p->Title;
$p->URLSegment = $p->Title;
$p->Content = '';
$p->Status = 'Published';
$p->write();
    $p->doRestoreToStage();
$p->publish('Stage', 'Live');
$p->flushCache();


How can I create a redirector page that redirects to / ?

I don't want a normal page, when someone goes to this URL I want them to be redirected to the home page.

Avatar
Willr

6 January 2011 at 8:43pm Forum Moderator, 5511 Posts

How can I create a redirector page that redirects to / ?
I don't want a normal page, when someone goes to this URL I want them to be redirected to the home page.

Well instead of creating a new Page(); create a new RedirectorPage();

You also then need to set the fields to point to the page it redirects to. Via

$p->RedirectionType = 'Internal';
$p->ExternalURLID = DataObject::get_one('SiteTree', "URLSegment = 'home'");

Avatar
DeklinKelly

7 January 2011 at 8:35am Community Member, 197 Posts

Perfect, thanks! Now how can I keep that item from showing up in the menu? I can't figure out how to use ShowInMenus with this.

Avatar
Willr

7 January 2011 at 6:56pm Forum Moderator, 5511 Posts

$p->ShowInMenus = 0; // or false should also work.

Avatar
pouic

28 July 2011 at 11:36am Community Member, 16 Posts

How can I validate the urlsegment page already exists before creating the dynamic page?