|
| 1 | +--- |
| 2 | +pageClass: twill-doc |
| 3 | +--- |
| 4 | + |
| 5 | +# Twill Capsules |
| 6 | + |
| 7 | +Twill capsules are modules that are packaged with other functionality. They can be used to improve your projects code |
| 8 | +organization, they can be part of [packages](../packages) or your own code base. |
| 9 | + |
| 10 | +You can even distribute capsules using Github. |
| 11 | + |
| 12 | +## Installing existing capsules |
| 13 | + |
| 14 | +You can find some capsules on the [Awesome Twill repository](https://github.com/pboivin/awesome-twill#capsules). |
| 15 | +Alternatively, you can search by the [`twill-capsule`](https://github.com/topics/twill-capsule) |
| 16 | +or [`twill-package`](https://github.com/topics/twill-package) tags on Github. |
| 17 | + |
| 18 | +To install an existing capsule that is hosted on Github, for example |
| 19 | +the [Twill cities capsule](https://github.com/area17/twill-capsule-cities) we can run the following command: |
| 20 | + |
| 21 | +``` |
| 22 | +php artisan twill:capsule:install |
| 23 | +``` |
| 24 | + |
| 25 | +There are a few important arguments we can use: |
| 26 | + |
| 27 | +- `--copy` makes a copy into your `app/Twill/Capsules` folder. |
| 28 | +- `--require` uses composer require to include the capsule |
| 29 | + |
| 30 | +The strategy to apply depends on your project. If you wish to make project specific changes to the capsule you should |
| 31 | +use the `--copy` method. Otherwise, if you are fine with how the capsule is, `--require` will do the trick. |
| 32 | + |
| 33 | +So the full command to install the cities capsule and copy it would be: |
| 34 | + |
| 35 | +``` |
| 36 | +php artisan twill:capsule:install area17/twill-capsule-redirections --copy --branch=main |
| 37 | +``` |
| 38 | + |
| 39 | +::: tip |
| 40 | +We add `--main` because at the time of writing the is not yet a release made for this capsule. Usually this is not |
| 41 | +needed. |
| 42 | +::: |
| 43 | + |
| 44 | +Once you have done that, we still need to add the capsule to our `config/twill-navigation.php` and enable it from our |
| 45 | +`config/twill.php` files. |
| 46 | + |
| 47 | +The exact setup might vary from capsule to capsule, so best is to always check the capsule you are working with to see |
| 48 | +if they provide a readme. |
| 49 | + |
| 50 | +In `config/twill.php`, tip: if you need to know the name, you can check the folder name in `app/Twill/Capsules` |
| 51 | + |
| 52 | +```php |
| 53 | +<?php |
| 54 | + |
| 55 | +return [ |
| 56 | + 'capsules' => [ |
| 57 | + 'list' => [ |
| 58 | + [ |
| 59 | + 'name' => 'Redirections', |
| 60 | + 'enabled' => true, |
| 61 | + ], |
| 62 | + ], |
| 63 | + ], |
| 64 | + ... |
| 65 | +]; |
| 66 | +``` |
| 67 | + |
| 68 | +And in `config/twill-navigation.php` |
| 69 | + |
| 70 | +```php |
| 71 | +return [ |
| 72 | + 'redirections' => [ |
| 73 | + 'title' => 'Redirections', |
| 74 | + 'module' => true, |
| 75 | + ], |
| 76 | +]; |
| 77 | +``` |
| 78 | + |
| 79 | +After this, make sure you run your migrations `php artisan migrate` and you should be good to go. Visit your Twill admin |
| 80 | +and the redirects module should be in the menu. |
| 81 | + |
| 82 | +## Creating capsules |
| 83 | + |
| 84 | +Before you dive into creating a capsule for your project, it is important to first decide whether you want to |
| 85 | +redistribute |
| 86 | +it or if it is a project local way of organizing the code. |
| 87 | + |
| 88 | +If you plan on redistributing, please head to the [packages](../packages) documentation as that is the new and preferred |
| 89 | +way to distribute Twill "plugins". |
| 90 | + |
| 91 | +If you do not need to distribute, read along! |
| 92 | + |
| 93 | +### Generating the capsule |
| 94 | + |
| 95 | +A capsule can be bootstrapped using the command `php artisan twill:make:capsule`, it accepts the same arguments as |
| 96 | +[the module cli generator](./cli-generator.md), if no arguments are passed it will ask during generation with the |
| 97 | +exception of `--singleton` which can be added if you want it to be a singleton module |
| 98 | + |
| 99 | +``` |
| 100 | +php artisan twill:make:capsule Comment |
| 101 | +``` |
| 102 | + |
| 103 | +Once the capsule is created, the cli output will tell you, similar to the above, what to add to |
| 104 | +your `config/twill-navigation.php` and `config/twill.php` files. |
| 105 | + |
| 106 | +`config/twill.php` |
| 107 | + |
| 108 | +```php |
| 109 | +<?php |
| 110 | + |
| 111 | +return [ |
| 112 | + 'capsules' => [ |
| 113 | + 'list' => [ |
| 114 | + [ |
| 115 | + 'name' => 'Comments', |
| 116 | + 'enabled' => true, |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ... |
| 121 | +]; |
| 122 | +``` |
| 123 | + |
| 124 | +And in `config/twill-navigation.php` |
| 125 | + |
| 126 | +```php |
| 127 | +return [ |
| 128 | + 'comments' => [ |
| 129 | + 'title' => 'Comments', |
| 130 | + 'module' => true, |
| 131 | + ], |
| 132 | +]; |
| 133 | +``` |
| 134 | + |
| 135 | +Now that the capsule is generated, you can go and modify the contents of `app/Twill/Capsules/Comments` to the needs of |
| 136 | +your project! |
0 commit comments