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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Create pages via config file


Go to End


3 Posts   843 Views

Avatar
anru

Community Member, 7 Posts

26 January 2016 at 10:44am

Hi:

Under normal condition, I have to create page manually on backend admin panel.
How can I create a page via Yml config file.

Please look at at this page, http://api.silverstripe.org/3.1/class-YamlFixture.html
It seems possible.

I ask this because of I do not want to create a page multiple time on local, testing, and live machine.

regards,

anru

Avatar
martimiz

Forum Moderator, 1391 Posts

27 January 2016 at 12:02am

Hi Anru,
I understand the confusion, but this YAML config is used in the testing environment to create a setup for testing purposes. So the test uses these pages instead of the actual pages from the database. So these settings aren't actually ever written to db.

You can however create pages using the requireDefaultRecords() function, for instance in your Page class. And it should be easy to set up a similar config in your YAML, and read that from this function.

Here's a really simple example, but google will give you more: http://www.silverstripe.org/community/forums/general-questions/show/21779

Avatar
anru

Community Member, 7 Posts

3 February 2016 at 2:58pm

Ok, Find a way to set up a site's basic page by code.

I have built a class extends BuildTask , in the class, I have used following code to create page.

if(class_exists('ContactPage')&& !DataObject::get_one('ContactPage')){
$page = new ContactPage();
$page->Title = _t("Contact");
$page->Content = '';
$page->URLSegment = 'contact';
$page->Status = 'Published';
$page->Sort = 1;
$page->write();
$page->doRestoreToStage();
$page->publish('Stage', 'Live');
$page->flushCache();
DB::alteration_message('Contact page created', 'created');
}

This way, I just have to run this task once, then all required pages are set up, and no need to manually create page.