A Basic Guide to Creating 'Page Types' in the Silvestripe CMS

'Page Types' form the core of your silverstripe sites, they can be very simple, or very very complicated! (We'll start with simple.)

There are basically three parts to creating page types

  1. Defining what Data we want on our page
  2. Defining what what we want the data to do
  3. Creating a template that displays this data

    The first two of these are dealt with in a PHP code file saved in the 'mysite/code' folder, and the third, the template, is a 'SS' file saved the 'themes' folder. We'll talk about themes/templates later.

    Page.php

    It's worth trying to understand that 'page types' in Silverstripe are always based on another parent 'page type'. Open up the file Page.php and you will see "Page extends SiteTree". SiteTree is a 'Class' - a set of code inside silverstripe's core files that controls all the basic page strucuture. 'Page' is based on (extends) 'SiteTree', and our new pages will be based on 'Page'.

    A New Page Type - 'HomePage'

    Create a new file called 'HomePage.php' in your 'mysite/code/ folder.

    <?php
    
    class HomePage extends Page {
    
    
    }
    
    
    class HomePage_Controller extends Page_Controller {
    
    	
    }

    Let's go through this code.

    There are two classes, at the moment both are empty. The first section 'HomePage' will define any data we want this page to use. 'HomePage_Controller' defines what we do with this data.

    Note that we've used the same name 'HomePage' for the file name itself, and to name these two classes.

    If you look back into 'Page.php' you will see the classes 'Page' and 'Page_Controller' are defined there. Our new page type simply extends 'Page', doing everything 'Page' does and more (if you want). 

    Simply creating the PHP file, and the two empty classes is acutally enough to create this page type. So let's create it.

    Our new page type doesn't just appear, we have to 'build' the database again.

    1. Make sure you have uploaded 'HomePage.php' to your webserver,
    2. Make sure you are logged in to your silverstripe website (mysilverstripesite.com/admin)
    3. Go to mysilverstripesite.com/dev/build
    4. You should see some lines of text, some of them green, and no errors
    5. Go to your admin area: mysilverstripesite.com/admin/ (reload it if it's already open)

    You should now be able to create a new page, and choose 'Home Page' from the list.

     

    If you don't need your page to have any extra data, then you're done with the code - you can start looking at creating a template for your page.

    I'll be creating a tutorial about adding more 'data' to your page soon.