|
| 1 | +# Control Panel |
| 2 | + |
| 3 | +Just like with Extension and Module routing, you have to extend your ExpressionEngine Addon file to use the corresponding Add-on Controller Base route. In this case, that'll be `ExpressionEngine\Service\Addon\Mcp`. |
| 4 | + |
| 5 | +You'll also have to add a new property to your Control Panel object called `$route_namespace` which is the namespace for route objects. |
| 6 | + |
| 7 | +> Note the below example uses the `setRouteNamespace` method to set the `$route_namespace` property. |
| 8 | +
|
| 9 | +### Update Control Panel File |
| 10 | + |
| 11 | +```php |
| 12 | +use ExpressionEngine\Service\Addon\Mcp; |
| 13 | + |
| 14 | +class My_addon_mcp extends Cp |
| 15 | +{ |
| 16 | + protected $module_name = 'my_addon'; |
| 17 | + |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + $this->setRouteNamespace('MyAddon\Addon\Controllers'); |
| 21 | + } |
| 22 | + |
| 23 | + public function index() |
| 24 | + { |
| 25 | + return $this->route('index', func_get_args()); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +#### About `$route_namespace` |
| 32 | + |
| 33 | +Note that `$route_namespace` isn't directly one to one. The Add-on Controller does some magic to keep things compartmentalized. Using the below example, the namespace used to locate your Route objects would be `Namespace\For\Your\Controllers\Cp\Routes`. |
| 34 | + |
| 35 | +#### About `$module_name` |
| 36 | + |
| 37 | +This value should contain the internal value ExpressionEngine uses to classify your parent Addon. |
| 38 | + |
| 39 | +> An extra special note about the use of `func_get_args()`: you should 100% keep doing that if you want Routing to be consistent. Basically, every declaration of a `route()` call should look like: `return $this->route('ROUTE_NAME', func_get_args());` |
| 40 | +
|
| 41 | +### Create Control Panel Routes |
| 42 | + |
| 43 | +Unlike any other Controller Route, Control Panel routes require an addition to your ExpressionEngine Control Panel object. The idea is you tell ExpressionEngine to your object then the Router takes over. For example, in the above example, we define an `index` method and then route it internally. |
| 44 | + |
| 45 | +The route object itself would look like the below: |
| 46 | + |
| 47 | +```php |
| 48 | +namespace YourAddon`\Addon\Controllers\Cp\Routes; |
| 49 | + |
| 50 | +use ExpressionEngine\Service\Addon\Mcp\AbstractRoute; |
| 51 | + |
| 52 | +class Index extends AbstractRoute |
| 53 | +{ |
| 54 | + protected $route_path = 'index'; |
| 55 | + |
| 56 | + public function process($id = false): AbstractRoute |
| 57 | + { |
| 58 | + return $this; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +You may have noticed that unlike traditional ExpressionEngine Control Panel methods, you don't return an array; you return an instance of your route instead. To generate output, the Router will take everything you setup within your object and pass that to ExpressionEngine. |
| 64 | + |
| 65 | +#### About `$route_path` |
| 66 | + |
| 67 | +The value MUST contain the `noun|/verb` your Route is accessed through. See the for more details. |
| 68 | + |
| 69 | +#### Full Example |
| 70 | + |
| 71 | +```php |
| 72 | +namespace YourAddon\Controllers\Mcp\Routes\ControllersExamples; |
| 73 | + |
| 74 | +use ExpressionEngine\Service\Addon\Mcp\AbstractRoute; |
| 75 | + |
| 76 | +class Cp extends AbstractRoute |
| 77 | +{ |
| 78 | + protected $route_path = 'controllers-examples/cp'; |
| 79 | + |
| 80 | + public function process($id = false): AbstractRoute |
| 81 | + { |
| 82 | + $this->setBody('test-route/my-action', []); |
| 83 | + $this->setHeading('cp'); |
| 84 | + |
| 85 | + $this->addBreadcrumb($this->url('controllers-examples'), 'eo.cp.nav.controller.examples'); |
| 86 | + return $this; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +## Helper Functionality |
| 93 | + |
| 94 | +### Sidebar Generator |
| 95 | + |
| 96 | +To automatically generate your sidebar, you'll need to add a property to your Route object called `$sidebar_data` that contains an array that outlines your sidebar. |
| 97 | + |
| 98 | +```php |
| 99 | +protected $sidebar_data = [ |
| 100 | + 'eo.cp.nav.controller.examples' => [ |
| 101 | + 'path' => 'controllers-examples', |
| 102 | + 'list' => [ |
| 103 | + 'cp' => 'controllers-examples/cp', |
| 104 | + 'module' => 'controllers-examples/mod', |
| 105 | + ] |
| 106 | + ], |
| 107 | + 'eo.cp.nav.forms' => [ |
| 108 | + 'path' => '', |
| 109 | + 'list' => [ |
| 110 | + 'eo.cp.nav.example' => 'forms/example' |
| 111 | + ] |
| 112 | + ], |
| 113 | + 'eo.cp.nav.members' => [ |
| 114 | + 'path' => '', |
| 115 | + 'list' => [ |
| 116 | + 'eo.cp.nav.example' => 'members' |
| 117 | + ] |
| 118 | + ], |
| 119 | + 'eo.cp.nav.entries' => [ |
| 120 | + 'path' => '', |
| 121 | + 'list' => [ |
| 122 | + 'eo.cp.nav.example' => 'entries' |
| 123 | + ] |
| 124 | + ] |
| 125 | +]; |
| 126 | +``` |
| 127 | + |
| 128 | +Note the `$active_sidebar` property can be used to specify a specific sidebar node as having an `active` state. Otherwise, that'll be determined through the `$route_path` property. |
| 129 | + |
| 130 | +### URL Helper |
| 131 | + |
| 132 | +To remove a considerable amount of keystrokes, you can use the `url($path, $with_base, $query)` method. What this does is allow you to create Control Panel URLs using the short syntax of a Route as well as any URL. |
| 133 | + |
| 134 | +```php |
| 135 | +protected function url(string $path, bool $with_base = true, array $query = []): string |
| 136 | +``` |
| 137 | +#### `$path` |
| 138 | + |
| 139 | +Either the Router shortname for the route you want, or a full URL to the destination. |
| 140 | + |
| 141 | +#### `$with_base` |
| 142 | + |
| 143 | +Boolean to compile the URL along with the `$base_url` property. If your link is to anything BUT a Route, you'll want this to be false. |
| 144 | + |
| 145 | +#### `$query` |
| 146 | + |
| 147 | +An array of key=>values you want to use for the query string on the URL. |
| 148 | + |
| 149 | +### Breadcrumbs |
| 150 | + |
| 151 | +You apply breadcrumbs using either the `addBreadcrumb($url, $text)` or the `setBreadcrumbs(array $breadcrumbs = [])` method(s). Note that `setBreadcrumbs` will completely reset any previously compiled `breadcrumb` items. |
| 152 | + |
| 153 | +```php |
| 154 | +protected function setBreadcrumbs(array $breadcrumbs = []): AbstractRoute |
| 155 | +protected function addBreadcrumb(string $url, string $text): AbstractRoute |
| 156 | +``` |
| 157 | + |
| 158 | +### Body Content |
| 159 | + |
| 160 | +To output content within the ExpressionEngine Control Panel, you have to dictate both a view script to use and the variables to pass to it. |
| 161 | + |
| 162 | +```php |
| 163 | +public function setBody(string $view, array $variables = []): AbstractRoute |
| 164 | +``` |
0 commit comments