Skip to content

Commit 39c1259

Browse files
committed
#1626: Add capsule documentation.
1 parent a415b39 commit 39c1259

File tree

4 files changed

+156
-4
lines changed

4 files changed

+156
-4
lines changed

docs/src/.vuepress/sidebar.js

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ module.exports = [
117117
{
118118
"title": "Nested Modules",
119119
"path": "/crud-modules/nested-modules.html",
120+
},
121+
{
122+
"title": "Capsules",
123+
"path": "/crud-modules/capsules.html",
120124
}
121125
],
122126
"collapsable": true

docs/src/crud-modules/capsules.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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!

src/Helpers/Capsule.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,21 @@ public function getRoutesFile(): string
282282
return $this->getPsr4Path() . '/routes/twill.php';
283283
}
284284

285-
public function routesFileExists(): bool
285+
public function getLegacyRoutesFile(): string
286286
{
287-
return file_exists($this->getRoutesFile());
287+
return $this->getPsr4Path() . '/routes/admin.php';
288+
}
289+
290+
public function getRoutesFileIfExists(): ?string
291+
{
292+
if (file_exists($this->getRoutesFile())) {
293+
return $this->getRoutesFile();
294+
}
295+
if (file_exists($this->getLegacyRoutesFile())) {
296+
return $this->getLegacyRoutesFile();
297+
}
298+
299+
return null;
288300
}
289301

290302
public function getModel(): string

src/TwillRoutes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ public function supportSubdomainRouting()
115115

116116
public function registerCapsuleRoutes($router, Capsule $capsule): void
117117
{
118-
if ($capsule->routesFileExists()) {
118+
if ($routesFile = $capsule->getRoutesFileIfExists()) {
119119
$this->registerRoutes(
120120
$router,
121121
$this->getRouteGroupOptions(),
122122
$this->getRouteMiddleware(),
123123
$this->supportSubdomainRouting(),
124124
$capsule->getControllersNamespace(),
125-
$capsule->getRoutesFile(),
125+
$routesFile,
126126
// When it is not a package capsule we can register it immediately.
127127
! $capsule->packageCapsule
128128
);

0 commit comments

Comments
 (0)