FusionCMS is currently in an open beta state and under heavy active development.

Theme Service Providers

As first class citizens, themes can tap into the core API of FusionCMS through the use of service providers. Service providers are the connection points between your theme and FusionCMS. They allows you to register advanced theme functionality and resources such as routes, views, configuration, and localization files.

You can learn all about the service container and service providers through Laravel's documentation.

Creating a Service Provider

Though themes generally do not have hard requirements in terms of folder structure, service providers are an exception. These files will need to be placed in a src/Providers folder within your theme in order to be accessible.

From there, create a new file ThemeServiceProvider.php and copy the following code into this file as a basic starting point.

// Note: please replace {MyTheme} with the namespace of your theme
<?php

namespace Themes\{MyTheme}\Providers;

use Illuminate\Support\ServiceProvider;

class ThemeServiceProvider extends ServiceProvider
{
    public function boot()
    {}

    public function register()
    {}
}

There are two important methods here, register and boot, that your service provider must implement. The register method is where you would register and bind PHP classes to the service container as a new service. You should never attempt to directly add event listeners, routes, or any other piece of functionality here. The boot method is where you can actually use your registered services. This method is called after all service providers have been registered.

Next, you'll need to register the service provider with your theme. Open your theme.json manifest file and add a providers section like the example below.

{
    "name": "My Theme",
    "namespace": "{MyTheme}",
    "description": "Theme description",
    "author": "Theme Author",
    "version": "1.0.0",
    "providers": [
        "Themes\\{MyTheme}\\Providers\\ThemeServiceProvider"
    ]
}

From this point you have the basic structure required to start using service providers! Continue reading on about adding custom routes or localization to your theme for examples on utilizing service providers.

Have questions?

We're always happy to help with code or other questions you might have. Contact support or chat live with us on Discord.

Last edited on Wednesday, July 15, 2020 (4 years ago)