Your first plugin in 10 minutes
Build a small page plugin, upload it, run it and change it, from start to end.
Have a local Exment ready first (Recommended development environment). You need to log in as a system administrator.
The flow
You will build this page:
- It shows the name of the logged-in user and how many "Information" records that user may see.
- When you send a name, it greets you back (an example of a form, CSRF and escaping).
1. Make the files
Make a folder HelloExment with these three files.
HelloExment/
├─ config.json
├─ Plugin.php
└─ resources/
└─ views/
└─ index.blade.phpconfig.json
{
"plugin_name": "HelloExment",
"plugin_view_name": "Hello Exment",
"description": "An example of a first plugin.",
"uuid": "e4aadce0-b85b-49f1-a0e5-23a3678acb7f",
"author": "(your name)",
"version": "1.0.0",
"plugin_type": "page",
"route": [
{
"uri": "",
"method": ["get"],
"function": "index"
},
{
"uri": "greet",
"method": ["post"],
"function": "greet"
}
]
}- Required:
plugin_name,plugin_view_nameanduuid. Without them the upload stops with an error. plugin_name: letters, digits and_.-and spaces cannot be used. It becomes the PHP namespace.uuid: the ID that tells plugins apart. Make a new one for your own plugin.
For example:uuidgenon macOS and Linux,[guid]::NewGuid()in Windows PowerShell.plugin_type:pageis a page plugin.route: pairs of a URL and the function to call. The GET with an emptyuriis called when the page is opened (if you leave it out,indexis called).
The other fields are in the Plugin reference.
Plugin.php
<?php
namespace App\Plugins\HelloExment;
use Exceedone\Exment\Model\CustomTable;
use Exceedone\Exment\Services\Plugin\PluginPageBase;
use ExmentAdminCore\Admin\Widgets\Box;
class Plugin extends PluginPageBase
{
/**
* GET (uri ""): the page is opened
*/
public function index()
{
return $this->page(null);
}
/**
* POST (uri "greet"): the form is sent
*/
public function greet()
{
// the name that was entered, cut when it is too long
$name = mb_substr(trim((string) request()->input('name', '')), 0, 50);
return $this->page($name === '' ? null : $name);
}
protected function page(?string $name)
{
// count only the "information" records the logged-in user may see
$table = CustomTable::getEloquent('information');
$count = (isset($table) && $table->hasPermission())
? $table->getValueQuery()->count()
: null;
return new Box($this->plugin->plugin_view_name, $this->pluginView('index', [
'user_name' => \Exment::user()->user_name,
'count' => $count,
'name' => $name,
'action' => admin_url($this->plugin->getRouteUri('greet')),
]));
}
}- The namespace is
App\Plugins\(plugin_name in Pascal case). If it does not match, the upload stops with an error. A PHP file in a subfolder adds the folder name too (e.g.App\Plugins\HelloExment\Services). - The class is named
Pluginand extendsPluginPageBase(the class for each type: Plugin reference). $this->pluginView('index', ...)usesresources/views/index.blade.php.getValueQuery()leaves out the data the user may not see.
resources/views/index.blade.php
<p>Hello, {{ $user_name }}.</p>
@if (!is_null($count))
<p>You can see {{ $count }} information records.</p>
@endif
<form method="POST" action="{{ $action }}">
@csrf
<p>
<label for="hello-name">Name</label>
<input type="text" id="hello-name" name="name" maxlength="50" class="form-control">
</p>
<button type="submit" class="btn btn-primary">Greet me</button>
</form>
@if (!is_null($name))
<p>Nice to meet you, {{ $name }}!</p>
@endif- Without
@csrf, the CSRF check stops the form when it is sent. - Values are output with double curly braces. HTML is escaped, so a
<script>in the name does not run.
2. Zip it
Zip the contents of the HelloExment folder (zipping the folder itself also works).
cd HelloExment
# macOS, Linux
zip -r ../HelloExment.zip config.json Plugin.php resources
# Windows 10 or later (Command Prompt or PowerShell)
tar -a -c -f ..\HelloExment.zip config.json Plugin.php resources
Compress-Archiveof Windows PowerShell 5.1 can write\as the folder separator. On a Linux server the folders then do not come out right. Usetar, or "Send to > Compressed (zipped) folder" in Explorer.
3. Upload it
- Open "Plugin" in the left menu (the URL is
http(s)://(Exment URL)/plugin). - Choose
HelloExment.zipwith [Choose File] and click [Upload]. - When "Hello Exment" appears in the list below, it is done.
If you get an error, check these.
| Error on the screen | Look at |
|---|---|
| A required field is missing | plugin_name, plugin_view_name and uuid in config.json |
| The namespace is wrong | namespace App\Plugins\HelloExment; in Plugin.php |
| config.json cannot be read | The JSON syntax (a trailing ,, for example) |
4. Run it
Open http(s)://(Exment URL)/plugins/hello_exment in your browser.
hello_exmentisplugin_namein snake case. You can change it with "URL" on the edit screen of the plugin.- On a site with "/admin" in the URL, it is
/admin/plugins/hello_exment. - To show it in the menu, add an item of the type "Plugin" in Menu.
- For users who are not system administrators, give "Access" in role groups, or set "All user can use" of the plugin to YES.
5. Change it
- Change a file (for example, a sentence in index.blade.php).
- Set
versionin config.json to1.0.1. - Zip it again and upload it on the same screen.
When plugin_name and uuid are the same, the upload replaces the plugin instead of adding a new one. The active setting and the plugin's own settings are kept. The fields written in config.json (such as the target tables) take the values of config.json.
When plugin_name is the same and uuid is different, you get an error.
For small, repeated changes on a local environment, you can also edit the folder of the uploaded plugin (storage/app/plugins/HelloExment/) directly. See Where the plugin is while you develop it.
Next
- Develop with AI coding tools
- How to build each type: Plugin creation method
