|
| 1 | +# Kirby Snippet Controller |
| 2 | +Define snippet controllers in a similar way how page controllers work. |
| 3 | +By default the plugin will try to find controllers in your `snippets` directory. |
| 4 | +Lets have a look at a example `header` snippet. |
| 5 | + |
| 6 | +## How to use it |
| 7 | +```bash |
| 8 | +📁 snippets |
| 9 | +├─ 📄 header.controller.php |
| 10 | +└─ 📄 header.php |
| 11 | +``` |
| 12 | + |
| 13 | +```php |
| 14 | +// header.controller.php |
| 15 | + |
| 16 | +// The return value can be a callback |
| 17 | +return function ($site) { |
| 18 | + return [ |
| 19 | + 'title' => $site->title(), |
| 20 | + ]; |
| 21 | +}; |
| 22 | + |
| 23 | +// Or you can simply return an array. |
| 24 | +return [ |
| 25 | + 'value' => site()->title(), |
| 26 | +]; |
| 27 | + |
| 28 | +``` |
| 29 | + |
| 30 | +You can also define controllers in a plugin. |
| 31 | +```php |
| 32 | +Kirby::plugin('superwoman/superplugin', [ |
| 33 | + 'snippets' => [ |
| 34 | + |
| 35 | + // Refer to a file |
| 36 | + 'header.controller' => __DIR__ . '/snippets/header.controller.php', |
| 37 | + |
| 38 | + // Return an array |
| 39 | + 'header.controller' => [ |
| 40 | + 'title' => site()->title(), |
| 41 | + ], |
| 42 | + |
| 43 | + // Return a callback |
| 44 | + 'header.controller' => function ($site) { |
| 45 | + return [ |
| 46 | + 'title' => $site->title(), |
| 47 | + ]; |
| 48 | + }, |
| 49 | + |
| 50 | + ], |
| 51 | +]); |
| 52 | +``` |
| 53 | + |
| 54 | +### Available callback arguments in your controllers |
| 55 | +Like in regular controllers, you can access the `$site`, `$page`, `$pages` and `$kirby` objects by loading them as arguments to the anonymous function. The plugin will inject the right objects for you. In addition, you also have access to the `$data` argument, which contains the data you passed to the snippet. |
| 56 | + |
| 57 | +### Naming convention |
| 58 | +By default, the plugin searches for controllers by appending `.controller` to the snippet name. You can change the name resolver in the options. Changing the name also affects plugin-defined controllers. |
| 59 | +```php |
| 60 | +// config.php |
| 61 | + |
| 62 | +return [ |
| 63 | + 'lukaskleinschmidt.snippet-controller' => [ |
| 64 | + |
| 65 | + // The default resolver |
| 66 | + 'name' => fn (string $name) => $name . '.controller', |
| 67 | + |
| 68 | + // You might want to store controllers in a separate folder |
| 69 | + 'name' => fn (string $name) => 'controllers/' . $name, |
| 70 | + |
| 71 | + ], |
| 72 | +]; |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +## Commercial Usage |
| 77 | +This plugin is free. Please consider to [make a donation](https://www.paypal.me/lukaskleinschmidt/5EUR) if you use it in a commercial project. |
| 78 | + |
| 79 | +## Installation |
| 80 | + |
| 81 | +### Download |
| 82 | +Download and copy this repository to `/site/plugins/snippet-controller`. |
| 83 | + |
| 84 | +### Git submodule |
| 85 | +``` |
| 86 | +git submodule add https://github.com/lukaskleinschmidt/kirby-snippet-controller.git site/plugins/snippet-controller |
| 87 | +``` |
| 88 | + |
| 89 | +### Composer |
| 90 | +``` |
| 91 | +composer require lukaskleinschmidt/kirby-snippet-controller |
| 92 | +``` |
| 93 | + |
| 94 | +## License |
| 95 | +MIT |
| 96 | + |
| 97 | +## Credits |
| 98 | +- [Lukas Kleinschmidt](https://github.com/lukaskleinschmidt) |
0 commit comments