# Plugin development guide

> Build a Leantime plugin from the Hello World example: composer.json, register.php, language files, controllers, Blade views and menu items.

Source: https://docs.leantime.io/development/plugin-development/

A plugin adds pages, menu items and event listeners to Leantime without changing core code. This page builds a small Hello World plugin that runs on Leantime 3.17. It uses the same layout as Leantime's own plugins, such as CalDAV and MCP Server.

## What does a plugin look like?

A plugin is one folder in `app/Plugins/`. The folder name is the plugin's ID, and it's part of every class name in the plugin.

```
app/Plugins/HelloWorld/
├── composer.json          # Name, version and the Leantime versions it supports
├── register.php           # Runs on every request: languages, menu items, listeners
├── Services/
│   └── HelloWorld.php     # Required: Services/{Folder}.php, with optional install()/uninstall()
├── Controllers/
│   └── Show.php           # The page at /helloworld/show
├── Templates/
│   └── show.blade.php     # The view "helloworld.show"
└── Language/
    └── en-US.ini          # Strings for __()
```

Three rules make this work:

- **Namespaces follow the folders.** Core's autoloader maps `Leantime\` to `app/`, so `app/Plugins/HelloWorld/Controllers/Show.php` must declare `class Show` in `namespace Leantime\Plugins\HelloWorld\Controllers`. The class name must match the file name. You don't need an `autoload` section in `composer.json`.
- **Views live in `Templates/`.** Leantime registers each plugin's `Templates/` folder under the folder name in lowercase. `HelloWorld/Templates/show.blade.php` is the view `helloworld.show` (or `helloworld::show`).
- **`Services/{Folder}.php` must exist.** When you activate a plugin, Leantime creates `Leantime\Plugins\{Folder}\Services\{Folder}`. Activation fails if that class is missing.

## Step 1: Write composer.json

```json
{
    "name": "acme/helloworld",
    "description": "Says hello on its own page",
    "version": "1.0.0",
    "type": "leantime-plugin",
    "license": "MIT",
    "homepage": "https://example.com/helloworld",
    "authors": [
        {
            "name": "Acme",
            "email": "dev@example.com"
        }
    ],
    "extra": {
        "leantime": {
            "requires": ">=3.17 <4.0"
        }
    }
}
```

- **`name`, `description`, `version`, `homepage` and `authors` are all needed.** Leantime reads them to list the plugin. If one is missing, the plugin doesn't appear under **Downloaded Plugins**, and `storage/logs/` shows "Can't create plugin from composer".
- **`extra.leantime.requires`** lists the Leantime versions the plugin supports, as a [Composer version constraint](https://getcomposer.org/doc/articles/versions.md). On any other version, Leantime doesn't load the plugin, shows it as **Not compatible** in **My Apps**, and won't enable it. A plugin that leaves it out counts as `>=3.0 <4.0`.
- **`name`** is also the name the command-line tools use, for example `php bin/leantime plugin:enable "acme/helloworld"`.

## Step 2: Add the service class

`Services/HelloWorld.php`:

```php
<?php

namespace Leantime\Plugins\HelloWorld\Services;

class HelloWorld
{
    /**
     * Runs once, when the plugin is activated. Create tables or default settings here.
     */
    public function install(): bool
    {
        return true;
    }

    /**
     * Runs when the plugin is removed. Delete what install() created.
     */
    public function uninstall(): bool
    {
        return true;
    }
}
```

Both methods are optional; the class isn't. Put the plugin's business logic in services like this one, and keep controllers thin.

## Step 3: Write register.php

Leantime includes `register.php` of every enabled plugin on every request, before the controller runs. Register languages, menu items and event listeners here.

```php
<?php

use Leantime\Domain\Plugins\Services\Registration;

$registration = app()->makeWith(Registration::class, ['pluginId' => 'HelloWorld']);

// Load every Language/*.ini file. Pass ['en-US', 'de-DE'] to load only some.
$registration->registerLanguageFiles();

// Add a link to the personal menu (Home, My Timesheets, My Calendar).
$registration->addMenuItem([
    'title' => 'helloworld.menu.title',
    'icon' => 'fa fa-face-smile',
    'tooltip' => 'helloworld.menu.tooltip',
    'href' => '/helloworld/show',
], 'personal', [25]);
```

- **`pluginId`** must be the folder name, with the same capitals.
- **The menu section** is one of `personal`, `company`, `projecthub` or `default` (the project menu).
- **The location** is the position in that menu. Core items in `personal` use 5, 7, 15 and 20, so 25 comes last. To add the link to a submenu, give the submenu's position first: `[15, 25]` in `company` puts it in **Administration**.
- **`title` and `tooltip`** are language keys.

## Step 4: Add the strings

`Language/en-US.ini`:

```ini
helloworld.menu.title = "Hello World"
helloworld.menu.tooltip = "A simple Hello World example"
helloworld.headline = "Hello World"
helloworld.text = "Hello from your first Leantime plugin!"
```

Add a file per language, named like the core ones (`de-DE.ini`, `fr-FR.ini`). English loads first, and the person's own language overrides it.

## Step 5: Add the controller

`Controllers/Show.php`:

```php
<?php

namespace Leantime\Plugins\HelloWorld\Controllers;

use Leantime\Core\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class Show extends Controller
{
    /**
     * Shows the Hello World page.
     */
    public function get(array $params): Response
    {
        $this->tpl->assign('message', __('helloworld.text'));

        return $this->tpl->display('helloworld.show');
    }
}
```

Leantime maps URLs to plugin controllers by name:

| URL | Runs |
| --- | --- |
| `GET /helloworld/show` | `Controllers\Show::get()` |
| `POST /helloworld/show` | `Controllers\Show::post()` |
| `GET /helloworld/show/42` | `Controllers\Show::get()`, with `$params['id']` = `42` |
| `GET /helloworld/show/preview` | `Controllers\Show::preview()` |
| `/hx/helloworld/show` | `Hxcontrollers\Show` (an HTMX fragment) |

- **The first segment** is the plugin folder name, in any case.
- **The second segment** is the controller class, in StudlyCase (`/helloworld/show-all` runs `ShowAll`).
- **Only enabled plugins** are routed. Everyone who is signed in can open the page. To limit it to some roles, check in an `init()` method the way the CalDAV plugin does: `Auth::authOrRedirect([Roles::$owner, Roles::$admin])`.
- **Inject services through `init()`,** not the constructor. Leantime calls `init()` with dependency injection after it builds the controller.

For a path that doesn't fit this pattern, add a `routes.php` file with ordinary Laravel routes. Leantime loads it for enabled plugins. The CalDAV plugin uses one for `/.well-known/caldav`.

## Step 6: Add the template

`Templates/show.blade.php`:

```blade
@extends($layout)

@section('content')

<x-global::layout.page-header icon="fa fa-face-smile">
    {{ __('helloworld.headline') }}
</x-global::layout.page-header>

<div class="maincontent">
    <x-global::layout.content-box>
        <p>{{ $message }}</p>
    </x-global::layout.content-box>
</div>

@endsection
```

`$this->tpl->display()` puts the page inside the normal Leantime layout. Use `$this->tpl->displayPartial()` for content without the layout, such as a modal.

## Step 7: Install and enable the plugin

1. Copy the `HelloWorld` folder into `app/Plugins/`. The web server user needs to be able to read it (see [Installing plugins](https://docs.leantime.io/installation/plugin-installation/)).
2. Check that it loads without errors:

   ```bash
   php bin/leantime plugin:verify HelloWorld
   ```

3. Go to **Company → Leantime Apps → My Apps**. The plugin is listed under **Downloaded Plugins**. Click **Activate**.
4. It moves to **Installed Plugins**. Click **Enable**.
5. Open **Hello World** in the personal menu, or go to `/helloworld/show`.

From the command line, use the `name` from `composer.json`:

```bash
php bin/leantime plugin:install "acme/helloworld"
php bin/leantime plugin:enable "acme/helloworld"
```

## How do I react to something that happens in Leantime?

Add a listener in `register.php`. Most core actions fire a class-based event that carries the IDs you need:

```php
use Illuminate\Support\Facades\Log;
use Leantime\Core\Events\EventDispatcher;
use Leantime\Domain\Tickets\Events\TicketCreated;

EventDispatcher::add_event_listener(TicketCreated::class, function (TicketCreated $event): void {
    Log::info('To-do created', ['ticketId' => $event->ticketId, 'projectId' => $event->projectId]);
});
```

See [Event hooks and filters](https://docs.leantime.io/technical/hooks/) for the list of events and filters, and how to find the rest.

## What else can register.php do?

| Method on `Registration` | What it does |
| --- | --- |
| `registerMiddleware([MyMiddleware::class])` | Adds HTTP middleware that runs after core's middleware. CalDAV uses it to create its tables on the first request. |
| `registerModule([...])` | Lets admins switch the plugin on or off per project, on the **Modules** tab of **Project Settings**. Its menu items and `/{plugin}/…` pages follow the switch. |
| `addCss([...])`, `addHeaderJs([...])`, `addFooterJs([...])` | Adds CSS or JavaScript to every page. The files must be built into the plugin's `dist/` folder with a `mix-manifest.json`; Leantime looks them up there. |

## Something doesn't work

- **The plugin isn't under Downloaded Plugins.** Check that `composer.json` is valid JSON and has all the fields from step 1. Look in `storage/logs/`.
- **Activate fails.** `Services/{Folder}.php` is missing, or its class name doesn't match the folder.
- **"Can't find a valid controller".** The plugin isn't enabled, or the class name doesn't match its file name or the URL.
- **"Template … not found".** The template isn't in `Templates/`, or the view name isn't the lowercase folder name. If you added `Templates/` after enabling the plugin, run `php bin/leantime cache:clearAll`.
- **The menu shows the language key, not the text.** `registerLanguageFiles()` is missing, or the key isn't in the `.ini` file.
- **The plugin shows as Not compatible.** The running Leantime version is outside `extra.leantime.requires`.

## Related pages

- [Installing plugins](https://docs.leantime.io/installation/plugin-installation/)
- [Event hooks and filters](https://docs.leantime.io/technical/hooks/)
- [Using the Leantime CLI](https://docs.leantime.io/development/commandline/)
- [Contributing](https://docs.leantime.io/development/contributing/)