|
| 1 | +--- |
| 2 | +title: Shortlinks |
| 3 | +tags: |
| 4 | + - Short link |
| 5 | + - Routing |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- cspell:ignore shortlink --> |
| 9 | +<!-- cspell:ignore shortlinks --> |
| 10 | +<!-- cspell:ignore ALPHANUMEXT --> |
| 11 | + |
| 12 | +Shortlinks are concise URLs that route to a fully-qualified URL in Moodle. Comprising of a smaller set of characters, their use is suitable for SMS, convenience, or where a longer URL would not be suitable. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +Shortlinks require Moodle's routing system to be enabled and fully functioning as they have been fully integrated into the existing routing subsystem. Ensure routing is configured by following this [routing guide](https://docs.moodle.org/500/en/Configuring_the_Router). |
| 17 | + |
| 18 | +## URL structure |
| 19 | + |
| 20 | +A shortlink URL will be comprised of the following: |
| 21 | + |
| 22 | +- Base domain |
| 23 | +- Route group of `s` or `p` |
| 24 | +- Shortcode (e.g. `X8fG56aa`) |
| 25 | + |
| 26 | +A complete shortlink will look something like this: `http://yourmoodle.com/s/X8fG56aa`. |
| 27 | + |
| 28 | +## Short codes |
| 29 | + |
| 30 | +Short codes are unique identifiers at the end of a shortlink. The characters available for building short codes is limited to the extended alpha-numeric set (ALPHANUMEXT) with the the omission of ambiguous characters (i.e. upper case 'i' and lower case 'L'). |
| 31 | + |
| 32 | +## Generating shortlinks |
| 33 | + |
| 34 | +Before a shortlink can be used, a shortlink will need to be generated. Shortlink generation is random and length can be specified when calling the relevant method. |
| 35 | + |
| 36 | +There are two types of shortlinks: |
| 37 | + |
| 38 | +- Public shortlinks |
| 39 | +- Private shortlinks |
| 40 | + |
| 41 | +### Public |
| 42 | + |
| 43 | +Public shortlinks can be accessed by anyone. Their use will be designated with a `/p/` in the URL. |
| 44 | + |
| 45 | +```php |
| 46 | +$public = \core\di::get(\core\shortlink::class)->create_shortlink( |
| 47 | + component: $component, |
| 48 | + linktype: $linktype, |
| 49 | + identifier: $identifier, |
| 50 | + userid: 0, |
| 51 | + minlength: 4, |
| 52 | + maxlength: 4, |
| 53 | +); |
| 54 | +``` |
| 55 | + |
| 56 | +### Private |
| 57 | + |
| 58 | +Private shortlinks are tied to a specific user or set of users. Their use will be designated with a `/s/` in the URL. |
| 59 | + |
| 60 | +```php |
| 61 | +$private = \core\di::get(\core\shortlink::class)->create_shortlink_for_users( |
| 62 | + component: $component, |
| 63 | + linktype: $linktype, |
| 64 | + identifier: $identifier, |
| 65 | + userids: $userids, |
| 66 | + minlength: 4, |
| 67 | + maxlength: 4, |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +## Using shortlinks |
| 72 | + |
| 73 | +Each component using shortlinks will need to have a `shortlink_handler` class. There are two methods that the implemented interface requires: |
| 74 | + |
| 75 | +- `get_valid_linktypes()` |
| 76 | +- `process_shortlink()` |
| 77 | + |
| 78 | +```php |
| 79 | +class shortlink_handler implements shortlink_handler_interface { |
| 80 | + #[\Override] |
| 81 | + public function get_valid_linktypes(): array { |
| 82 | + return [ |
| 83 | + 'view', |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + #[\Override] |
| 88 | + public function process_shortlink( |
| 89 | + string $type, |
| 90 | + string $identifier, |
| 91 | + ): ?\core\url { |
| 92 | + return match ($type) { |
| 93 | + 'view' => new \core\url('/mod/assign/view.php', [ |
| 94 | + 'id' => $identifier, |
| 95 | + ]), |
| 96 | + default => null, |
| 97 | + }; |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +Assuming the shortlink has been generated, the above example would allow a shortlink URL of `http://yourmoodle.com/s/SHORTCODE` to map to `http://yourmoodle.com/mod/assign/view.php?id=IDENTIFIER` |
| 103 | + |
| 104 | +## See also |
| 105 | + |
| 106 | +- [Routing](../routing/index.md) |
0 commit comments