You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/5.x/extend/element-types.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -936,7 +936,9 @@ public function getUriFormat(): ?string
936
936
}
937
937
```
938
938
939
-
URIs are rendered and stored on a per-site basis. Whenever an element’s URL is requested, Craft will instantiate the element and call its `getRoute()` method. Internally, <craft5:craft\base\Element::getRoute()> will call a protected `route()` method, which is what you should implement in your element class:
939
+
URIs are rendered and stored on a per-site basis.
940
+
Whenever an element’s URL is requested, Craft will instantiate the element and call its `getRoute()` method.
941
+
Internally, <craft5:craft\base\Element::getRoute()> will call a protected `route()` method, which is what you should implement in your element class:
940
942
941
943
```php
942
944
protected function route(): array|string|null
@@ -953,6 +955,42 @@ protected function route(): array|string|null
953
955
}
954
956
```
955
957
958
+
Your route can map to any [controller action](controllers.md), including ones in your plugin’s namespace—handy when you want to organize additional view logic:
959
+
960
+
```php
961
+
protected function route(): array|string|null
962
+
{
963
+
return [
964
+
// Route to `mynamespace\myplugin\controllers\StoreController::actionView()`:
965
+
'plugin-handle/store/view',
966
+
[
967
+
'product' => $this,
968
+
],
969
+
];
970
+
}
971
+
```
972
+
973
+
The second item in the rule array is treated as [route params](guide:structure-controllers#action-parameters), and will be automatically mapped to arguments of the same name in your action:
974
+
975
+
```php
976
+
public function actionView(Product $product): Response
977
+
{
978
+
// Load a user-defined template for this product type:
0 commit comments