| description | All admin controllers extend the base controller. |
|---|
Every admin controller extends Nails\Admin\Controller\Base, either directly or through DefaultController. Base is abstract and implements Nails\Admin\Interfaces\Controller, so the one method you must write is announce().
<?php
// src/Admin/Controller/Report.php
namespace App\Admin\Controller;
use App\Admin\Permission;
use Nails\Admin\Constants;
use Nails\Admin\Controller\Base;
use Nails\Admin\Factory\Nav;
use Nails\Factory;
class Report extends Base
{
public static function announce(): Nav|array|null
{
/** @var Nav $oNav */
$oNav = Factory::factory('Nav', Constants::MODULE_SLUG);
$oNav
->setLabel('Reports')
->setIcon('fa-chart-bar');
if (userHasPermission(Permission\Report\View::class)) {
$oNav->addAction('Sales Report', 'sales');
}
return $oNav;
}
public function sales(): void
{
if (!userHasPermission(Permission\Report\View::class)) {
unauthorised();
}
$this
->addBreadcrumb('Reports')
->addBreadcrumb('Sales')
->setData('aRows', $this->getSalesData())
->loadView('sales');
}
}When Admin creates your controller, Base::__construct():
- Fires the
ADMIN:STARTUPevent. - Links
$this->datato the main controller's data, so anything you set there reaches the view. - Loads the optional app configs
application/config/admin.phpandapplication/modules/admin/config/admin.php. - Clears any front-end assets, then loads Admin's own CSS and JS and the bundled libraries (jQuery, jQuery UI, Select2, CKEditor, Knockout, Moment, Mustache, Bootstrap, Font Awesome).
- Loads anything components ask for in their
autoloaddata (see below). - Fires the
ADMIN:READYevent.
If you override the constructor, call parent::__construct() first.
A component can ask Admin to load services, models, helpers, JS or CSS on every admin page by adding an autoload block for nails/module-admin to the extra.nails.data section of its composer.json:
{
"extra": {
"nails": {
"data": {
"nails/module-admin": {
"autoload": {
"helpers": ["book"],
"assets": {
"js": ["admin.min.js"],
"css": ["admin.min.css"],
"jsInline": ["console.log('admin ready');"]
}
}
}
}
}
}
}The supported keys are services, models, helpers, and assets with js, jsInline, css and cssInline.
announce() is static. Admin calls it on every discovered controller to build the sidebar, so keep it cheap. It returns a Nav, an array of Nav objects (to add links to several groups), or null (no sidebar presence). Controllers covers groups, icons, alerts and ordering.
Admin doesn't check permissions for you on a Base controller. Check them yourself, in two places:
- in
announce(), so users only see links they can use - in each method, because a user might visit a URL directly
Permissions are classes that implement Nails\Admin\Interfaces\Permission. Test them with userHasPermission():
if (!userHasPermission(Permission\Report\View::class)) {
unauthorised();
}See User Permissions for how to define them.
| Method | Purpose |
|---|---|
static url(string $sUrl = ''): string |
The absolute URL to this controller, with $sUrl appended. Follows overrides. |
setTitles(array $aTitles) |
Sets the page header and document <title>. Admin is always added as the first segment. |
addBreadcrumb(string $sLabel, ?string $sUrl) |
Adds a crumb to a linked breadcrumb trail. Replaces the title-based header. |
prependBreadcrumb(string $sLabel, ?string $sUrl) |
Inserts a crumb straight after the Admin root crumb. |
setData(string $sKey, mixed $mValue) |
Makes a variable available to the view. |
loadView(string $sView) |
A chainable shortcut for Helper::loadView(). |
Every method except url() returns $this, so calls can be chained.
setTitles() sets the header as a list of segments:
$this->setTitles(['Books', 'Reviews']); // Admin › Books › ReviewsTo make individual segments into links, use addBreadcrumb() instead. See Breadcrumbs.