October CMS resources and help articles

Simple and to the point. Optimized by the community.

Create Cms Page from PHP code

4
by mjauvin, last modified on September 3rd, 2020

Use this code to programatically create a CMS Page with URL/Title translations:

    $page = new \Cms\Classes\Page([
        'fileName' => 'test-page',
        'title' => 'My Page Title',
        'url' => '/test-page',
        'layout' => 'default',
    ]);

    $page->attributes['viewBag'] = [
            'localeUrl' => [
                    'fr' => '/french-URL',
                    'de' => '/deutch-URL',
            ],
            'localeTitle' => [
                    'fr' => 'French Title',
                    'de' => 'Deutch Title',
            ],
    ];

    $page->save();

(Note: to be used with Rainlab.Translate plugin)

Use this code to programatically create a CMS Page and attach a component:

    $page = new \Cms\Classes\Page([
        'fileName' => 'test-page',
        'title' => 'My Page Title',
        'url' => '/test-page',
        'layout' => 'default',
    ]);

    $page->attributes['myComponent'] = [];

    $page->save();

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?