Admin Panel
Modules can directly interact with the engine's admin panel.
To achieve this, the following steps are required:
- Define an item in the sidebar of the admin panel.
- Define admin panel routes.
- Define controllers.
- Define a
view
for each page within the admin panel.
Sidebar
For clarity, we will use a ServiceProvider named AdminServiceProvider, which is responsible for initializing the module within the admin panel.
Each item must have a structure (additional
):
[
'icon' => 'ph-cube', // Icon of your item
'title' => 'mon_admin.title', // Title. Translation keys can be used
'items' => [
[
'title' => 'Something',
'url' => '/admin/module/list' // Link where the subitem leads
],
]
],
To define our items in the sidebar, it's enough to call one class in the service provider:
public function register() : void
{
AdminSidebarBuilder::add(
'additional',
[
'icon' => 'ph-cube',
'title' => 'Our Item Title',
'items' => [
['title' => 'Something', 'url' => '/admin/module/list'],
]
],
);
}
Arguments of the add()
function:
- $key - Key in the sidebar. Can be
main
oradditional
. - $item - Array with data about the items.
- $permission - The permission needed to view this item.
Routes and Controllers
Next, we need to define our admin routes in our ServiceProvider.
Routes
In the routes, we constantly need to check for certain user permissions accessing our routes. This can be easily done - call middleware with our permission and then write our routes.
Here's an example:
router()->group(function (RouteGroup $router) {
// Checking permissions for our module
HasPermissionMiddleware::permission('admin.module');
$router->middleware(HasPermissionMiddleware::class);
// Our routes
$router->get('/list', [AdminViewController::class, 'list']);
$router->post('/add', [AdminApiController::class, 'add']);
}, 'admin/module');
We simply create routes with the prefix admin/, nothing complex.
Controllers
Controllers are absolutely no different from other controllers. An implementation example below:
class AdminApiController extends AbstractController
{
public function __construct()
{
// Checking for CSRF
this->middleware(CSRFMiddleware::class);
}
public function add(FluteRequest $request)
{
/** Implement desired functionality here */
}
}
Interface
Each admin panel interface should @extends
the main template of the admin panel and then add its content. Example:
@extends('Core.Admin.Http.Views.layout', [
'title' => 'Title of Our Page',
])
@push('header')
<!-- We can define our styles here -->
@endpush
@push('content')
<div class="admin-header">
<h2>Page Header</h2>
<p>Description of our page</p>
</div>
<!-- Some content -->
@endpush
@push('footer')
<!-- Our JS can be loaded here -->
@endpush
Certainly, in all arguments, you can insert translations through @t()
or __()
.
You can view examples of page implementations in app\Core\Admin\Http\Views\pages