Loading...
September 9, 2014#

Working with SilverStripe without the CMS

Most users, and even developers only really Silverstripe as a great CMS (Content Management System). While that is true, it also a framework and can be used even without the CMS module installed.

Why?

Well this isn’t for everyone that use Silverstripe, but sometimes its nicer not to have the overhead of the CMS for something you are working with, especially if its more web app based rather than a website. Also if you really like the admin area of Silverstripe you needn’t worry as it is now mostly baked into the Framework module so removing the CMS doesn’t remove this feature as of SS 3.0. The biggest benefit in the CMS is maintaining the menu(and of course the page content), so if you really don’t do that all that much than working without it might be an idea..

In Silverstripe 3.1, it is now way easier to do this, especially if you are using composer, just don’t include it in your composer.json file!

Now we need to do add a few things to our installation so we can start working in the framework.

Routes!

As we don’t have any CMS to help us look up pages, in the DB we need to tell SS where to grab our content. Here is an example of a routes.yml (place this in _config folder in your mysites or project folder) file of a simple 3 page site.

---
After: framework
---
Director:
rules:
"": HomeController
"home": HomeController
"about": AboutController
"contact": ContactController

Once we have these in place, and after /?flush=all we need to create our controllers to render some content.

Controllers

Like any MVC framework, Silverstripe uses Controllers to dispatch and return the work of our application. Above we would need to create 3 new Controllers named ‘HomeController’, ‘AboutController’ and ‘ContactController’ which all extend Controller.

e.g.

1
2
3
4
5
6
<?php
 class HomeController extends Controller {
  public function index() {
    echo 'Hello World';
  }
 }

The ‘index’ function is called by default on a controller, so now if you were to visit http://yourwebsite.blah/home, you should now see something.. of course with a little help from our friends ‘flush’ and ‘all’ you’d see something pop up in your browser.

Menu??

Without the CMS you don’t have access to creating and editing a menu. To get around this I would also recommend creating one more controller which oversees our controllers above. Lets call this SiteController, and this can house the menu system.

&lt;?php
class SiteController extends Controller {
 
 public function Menu() {
 
  $menu = new ArrayList();
  $siteClasses = ArrayLib::flatten(ClassInfo::subclassesFor('SiteController'), false);
  foreach ($siteClasses as $siteClass) {
   $inst = singleton($siteClass);
   $active = false;
 
   foreach (self::$controller_stack as $candidate) {
    $active = $candidate instanceof $arcClass;
   }
   $menu-&gt;push(new ArrayData(array(
   'Title' =&gt; $inst-&gt;Title(),
   'Link' =&gt; $inst-&gt;Link(),
   'Active' =&gt; $active,
   'Sort' =&gt; $inst-&gt;menuSort()
   )));
  }
  return $menu-&gt;sort('Sort ASC');
 
 }
}

You can then loop the menu in your SS template file as you would normally. e.g.

      <% loop $Menu %>

 

  • class=”current”<% end_if %>>$Title <% end_loop %>

That is all.

Please keep in mind this is a VERY barebones example of how to get things in Silverstripe working without the CMS installed. I plan on releasing a module to help people get off the ground faster from a few tricks I have learnt with the current project I am working. Please get in touch in the mean time if you would like to ask questions as I know there is very little documentation out there on the topic of Framework only work in Silverstripe.

2 Comments

  1. Hey man, I followed your blog / guide and found it useful. It was the only one that seemed to make any sense with routing, got lost at the templates stage though any ideas on how you set up the ss files for the controller when theres no cms and using just routes.

    Regards
    David

  2. Hey, thanks for your comment!

    I’d suggest looking at the getViewer() function on Controller. I like to setup a base controller that overloads this function with my own template implementation and then extend this base class with any frontend controllers so they have access to it. In this function you could implement logic that looks for a particular Model template file that is attached to that controller. So for e.g. you have a PersonController which has a Person DataObject bound to it, you could then use logic in the above function to find Person.ss which will render as the Layout.

    I want to at some stage release my base controller and associated extensions as a module that will allow people to quickly setup all this basic infrastructure for any framework only projects.

    Let me know if you have any questions.

    Good luck!

Leave a Comment