So, I'm developing a new extension right now, which needs access to the PHP version.
Obviously, you can't do that with just javascript.

I looked for ways to pass data from the back-end to the administration front-end, but even a custom route seemed too complex for just one value.
I just discovered a way on how to do this very easily (it can only pass values to the admin interface right now), and I would like to share it with you.


The Event PrepareUnserializedSettings

The Event we need is PrepareUnserializedSettings. This event has a property of settings which will be passed on to the forum object, although just in the admin page, which is the only place where a settings property is in the Flarum Forum Object. Yeah, kind of confusing ?

1. Setting Up The Event Listener

So, we create a new file for the event, in this case src/Listeners/PassDataToAdmin.php.
In bootstrap.php, of course, subscribe the event with something like this:

<?php

namespace Thing\Extension;

use Illuminate\Contracts\Events\Dispatcher;

return function (Dispatcher $events) {
    $events->subscribe(Listeners\PassDataToAdmin::class);
};

This is an example of what the content should kind-of look like in PassDataToAdmin.php`:

<?php

namespace Thing\Extension\Listeners;

use Flarum\Event\PrepareUnserializedSettings;
use Illuminate\Contracts\Events\Dispatcher;

class PassDataToAdmin {
    /**
     * Subscribes to the Flarum events
     *
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events) {
        $events->listen(PrepareUnserializedSettings::class, [$this, 'prepareUnserializedSettings']);
    }
    
    /**
     * Adds settings to admin settings
     * 
     * @param PrepareUnserializedSettings $event
     */
    public function prepareUnserializedSettings(PrepareUnserializedSettings $event) {
            $event->settings['phpVersion'] = phpversion();
    }
}

In this case, we are passing the setting phpVersion to the admin front-end.

2. Getting The Value(s)

To get the values from our custom properties on the front-end, all we need to do is

app.settings['phpVersion'] // or app.settings.phpVersion

replacing phpVersion, with, of course, our own property.


Thanks for viewing this tutorial! ?

Devs, if there is any way to do this, but to not just the admin view, please say so, and I'll try to either update this post or make a new one.
Also, I didn't know where to put this tutorial. Sorry ?


EDIT 5/17: Changed listener file name to PassDataToAdmin

datitisev changed the title to How to pass PHP variables to the admin front-end easily .

Very nice.

However, the example code will probably not work. The listener class name should not be the same as the event - PrepareUnserializedSettings. You either have to rename the listener or alias the event class when importing it with use.

    Franz I used Listener\PrepareUnserializedSettings on the subscribe event.

    Thanks, though, for the feedback! I'll keep that in mind.

      datitisev The problem is this line in your listener file:

      $events->listen(PrepareUnserializedSettings::class, [$this, 'prepareUnserializedSettings']);