Time for a new update. The local extenders I mentioned before luceos can be added by enriching the skeleton (flarum/flarum) - the thing where your extend.php
is located. To do so you can update your composer.json by adding a new key (on the same level as require
) called autoload
with the following entries:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
Run composer dumpautoload
after changing the file to enact the change.
If autoload is the last entry before the ending }
, make sure to remove the ,
. Composer will complain if the json file is invalid. Usually this is the cause.
You are now able to store php classes in the app
, next to public
and storage
.
One of the things we do this for is to enable a session driver that uses redis or the database. Flarum by default uses a file based session driver; when you want to allow visitors to hit all web nodes you need to share those sessions between the nodes. File based sessions are only stored on that node (unless you mount that directory on a shared disk, but that limits where you can host those web nodes).
In order to override that we can do the following:
app\Session\OverrideSessionHandler.php
<?php
namespace App\Session;
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Session\DatabaseSessionHandler;
class OverrideSessionHandler implements ExtenderInterface
{
public function extend(Container $container, Extension $extension = null)
{
$container->extend('session.handler', function ($_, $container) {
$config = $container->make('config');
return new DatabaseSessionHandler(
$container->make(ConnectionInterface::class),
'sessions',
$config['session.lifetime']
);
});
}
}
And in extend.php
:
return [
// ...
new App\Session\OverrideSessionHandler,
];
That's it 😉
Do let me know if you tried this out and what you think of it 👍️
I totally forgot about one thing, you need the sessions table. So here goes part 2 😉
Create a directory called resources/migrations
in your flarum directory. And store this file in it:
0000_00_00_000000_sessions_table.php
<?php
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
return Migration::createTable(
'sessions',
function (Blueprint $table) {
$table->string('id')->primary();
$table->unsignedInteger('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity')->index();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
}
);
This change to the database sadly isn't automatically picked up by Flarum, so we need a command to run the migration:
app/Console/MigrateCommand
:
<?php
namespace App\Console;
use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Paths;
use Illuminate\Contracts\Container\Container;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
class MigrateCommand extends AbstractCommand
{
/**
* @var Container
*/
protected $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
parent::__construct();
}
/**
* Fire the command.
*/
protected function fire()
{
/** @var ExtensionManager $manager */
$manager = $this->container->make(ExtensionManager::class);
$migrator = $manager->getMigrator();
$migrator->setOutput($this->output);
/** @var Paths $paths */
$paths = $this->container->make(Paths::class);
$migrator->run($paths->base . '/resources/migrations');
}
protected function configure()
{
$this
->setName('local:migrate')
->setDescription('Run outstanding local migrations');
}
protected function localDisk()
{
$driver = new Local(app(Paths::class)->base);
$filesystem = new Filesystem($driver);
return new FilesystemAdapter($filesystem);
}
}
Now register this command in your extend.php
:
return [
// ...
(new Flarum\Extend\Console)->command(App\Console\MigrateCommand::class),
];
Now run php flarum local:migrate
to create your sessions table 😉