Please note this is a quick draft from the top of my head, because the question came up so often, please add feedback if something is amiss.
If you want to create your own extension on your Flarum installation, you'll encounter the issue that your extension is not recognized if you simply create the directory under extensions. That is because composer doesn't know about it.
A common error message you will see is:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class <YourClass> does not exist' in <SomePathToContainer> ...
This tutorial will explain how to set up your development environment to allow any extensions you work on to be picked up by composer.
This tutorial assumes you have installed composer globally and that you have access to ssh to run its commands.
Terminology
- root/docroot - the install path of Flarum
- workbench - the directory where your extensions for developing are located
- package - the package as provided on packagist, with the name being a unique identifier, for instance
flarum/core
and hyn/guardian
.
- vendor - the author of the package, which is always the first part of a package name; flarum/core, hyn/guardian
The development directory
Create a directory directly under your docroot called workbench
. This is the directory we will place your extensions under.
Update composer.json
Edit the composer.json of Flarum in the docroot and add under require the following:
"repositories": [
{
"type": "path",
"url": "workbench/*/"
}
],
The composer json would now look like this:
{
"name": "flarum/flarum",
"description": "Delightfully simple forum software.",
"type": "project",
"keywords": ["forum", "discussion"],
"homepage": "http://flarum.org",
"license": "MIT",
"authors": [
{
"name": "Toby Zerner",
"email": "toby.zerner@gmail.com"
},
{
"name": "Franz Liedke",
"email": "franz@develophp.org"
}
],
"support": {
"issues": "https://github.com/flarum/core/issues",
"source": "https://github.com/flarum/flarum",
"docs": "http://flarum.org/docs"
},
"require": {
"flarum/core": "^0.1.0",
"flarum/akismet": "^0.1.0",
"flarum/approval": "^0.1.0",
"flarum/auth-facebook": "^0.1.0",
"flarum/auth-github": "^0.1.0",
"flarum/auth-twitter": "^0.1.0",
"flarum/bbcode": "^0.1.0",
"flarum/emoji": "^0.1.0",
"flarum/english": "^0.1.0",
"flarum/flags": "^0.1.0",
"flarum/likes": "^0.1.0",
"flarum/lock": "^0.1.0",
"flarum/markdown": "^0.1.0",
"flarum/mentions": "^0.1.0",
"flarum/pusher": "^0.1.0",
"flarum/sticky": "^0.1.0",
"flarum/subscriptions": "^0.1.0",
"flarum/suspend": "^0.1.0",
"flarum/tags": "^0.1.0"
},
"repositories": [
{
"type": "path",
"url": "workbench/*/"
}
],
"require-dev": {
"franzl/studio": "0.10.x@dev"
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "beta"
}
Adding/creating your extension to the workbench
Existing extension
In case you already have an extension in git (github). Just go to the workbench directory with ssh and run the command to create a clone of your code:
git clone <repository>
This will automatically create a directory under workbench with the name of your repository (without the vendor name). Remember the package name in your composer.json
file.
New extension
Create a new directory under workbench, make sure it follows all guidelines for an extension. Especially the composer.json
file is now important for the repository-path method to work, the name specifies your package name, remember this!
Register the package in your Flarum
Now once again edit the composer.json
in the docroot. If your package name is not mentioned under require, add it. The value should be *@dev
. Now with ssh run composer update
and you should see that your package is now symlinked from the workbench directory.
Issues
- If you already had your package in the composer.json and an entry exists in the vendor folder for your package, make sure you delete that vendor subfolder before running composer update in the last step.