Jump to:

23479 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Adding page from controller

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: 650 Views
  • FullWebService
    Avatar
    Community Member
    38 Posts

    Adding page from controller Link to this post

    Hi,

    I'm trying to add a page to the Sitetree from a controller.
    I have ProductGroup which extends Page and I'm able to make ProductGroup pages in the CMS.

    My code:

    $pg = new ProductGroup;
    $pg->Title = "Test";
    $pg->ParentID = 2;
    $pg->write();

    This makes a valid entry in both Sitetree_Live and ProductGroup in the database and I can view that page in the website. It also makes an entry in Sitetree, but with the ClassName SiteTree, no Title and no ParentID (ParentID = 0). It kinda mucks things up in the CMS. The page is there, but in the root and not under node 2. It doesn't have an URLSegment so it's hard to click on and it's uneditable.

    Is there any way I can have the Sitetree behave the same as Sitetree_Live?

    TIA

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: Adding page from controller Link to this post

    With writing pages you need 2 lines. One to write, the other to sync the stage / live modes

    $page->write();
    $page->publish("Stage", "Live");

    It doesn't have an URLSegment so it's hard to click on and it's uneditable.

    So then you probably need to set its URLSegment when you create the object.

  • FullWebService
    Avatar
    Community Member
    38 Posts

    Re: Adding page from controller Link to this post

    Thanks for the reply. The URLSegment is generated automatically so I don't have to set it. It just wasn't working in the Stage table.

    I had to reverse Stage and Live in the publish function, as it was going well for the Live bit, but not the Stage bit.

    $pg->publish("Live", "Stage");

    made it work!

    Thanks again.

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