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   949 Views

Avatar
DeklinKelly

Community Member, 197 Posts

6 January 2011 at 4:18pm

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

Forum Moderator, 5513 Posts

6 January 2011 at 8:43pm

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

Community Member, 197 Posts

7 January 2011 at 8:35am

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

Forum Moderator, 5513 Posts

7 January 2011 at 6:56pm

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

Avatar
pouic

Community Member, 19 Posts

28 July 2011 at 11:36am

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