Module
class Module
Generic Eladmin module.
Features:
- define module's apearence in admin interface - $title, $icon, title(), icon()
- handles authorization - $roles, $actionRoles, auth(), getRoles(), setRoles()
- handles rendering - requestUrl(), assetUrl(), view(), asset(), views(), blade()
- user defined actions - action
(), actions(), hasAction()
To configure the module you may define properties as shown in the following example.
class MyModule extends Eladmin\Module {
// set module's name
protected $title = 'My module';
// set module's icon
protected $icon = '<i class="fas fa-puzzle-piece"></i>';
// set authorized roles.
protected $roles = ['admin', 'user'];
// override to set authorized roles. empty array means any role.
protected $actionRoles = ['read' => self::ANYONE, 'write' => ['admin'], 'delete' => self::NOONE];
}
Views directory structure:
render.blade.php - module content
assets/style.css - module style
assets/script.css - module scripts
Constants
ACTION_PREFIX |
Action methods prefix |
Properties
protected | $title | override to set module's name | |
protected | $icon | override to set module's icon | |
protected | $views | Add views directory to module; | |
protected | $roles | override to set authorized roles | |
protected | $actionRoles | override to set authorized roles | |
private | $core | Eladmin core instance | |
private | $elakey | Module's elakey | |
private | $actionsParsed | normalizing action names for elaActionRoles is expensive, we want to do it only once |
Public Methods
Each module has to be initialized with eladmin instance and its own elakey.
Each module has its elakey - index in modules array - used to address requests.
Check if user is authorized to do action, or athorized to access module at all.
Create asset url, file path relative to /assets directory. Default $version = eladmin version
Get roles authorized to work with the module, or specific action. Empty array means any role is authorized.
We want action keys to be case insensitive. This converts action to lowercase.
Convinient method for plain text output. Sets HTTP header text/plain and echo $str.
Convinient method for html output. Sets HTTP header text/html and echo $str.
Convinient method for json output. Sets HTTP header application/json and echo serialized $json.
Protected Methods
Private Methods
Set roles authorized to work with the module, or specific action. Empty array means any role is authorized.
Details
at line 96
__construct($core, $elakey)
Each module has to be initialized with eladmin instance and its own elakey.
at line 104
final Core
core()
Return core instance
at line 111
final string
elakey()
Each module has its elakey - index in modules array - used to address requests.
at line 118
final bool
auth(string|null $action = null)
Check if user is authorized to do action, or athorized to access module at all.
at line 133
string
title()
Get name of the module.
at line 140
string
icon()
Get icon of the module.
at line 147
final string
requestUrl(string|null $action = null, array $args = [])
Return url for this module.
at line 160
final string
assetUrl(string $path, string|null $version = null)
Create asset url, file path relative to /assets directory. Default $version = eladmin version
at line 167
void
prepare()
Runs before any action is executed.
at line 174
final array|null
roles(string|null $action = null)
Get roles authorized to work with the module, or specific action. Empty array means any role is authorized.
at line 195
private void
setRoles(array|null $roles, string|null $action = null)
Set roles authorized to work with the module, or specific action. Empty array means any role is authorized.
at line 216
protected array
views()
Extends array of directories of views and assets.
Example:
public function views() : array {
return array_merge([__DIR__ . '/my_module'], parent::views());
}
at line 228
protected Blade
blade()
Return Blade instance
at line 235
final string
render(string $template, array $args = [])
Return rendered view.
at line 243
void
renderAsset(string $path)
Determinate asset content-type and render it.
at line 250
final static array
actions()
Return array of all defined actions.
at line 267
final static bool
hasAction(string $action)
Check if actions is defined.
at line 274
final static bool
isAction(string $action)
Check if actions is executed.
at line 281
final static string
parseAction(string $action)
We want action keys to be case insensitive. This converts action to lowercase.
at line 288
final static void
renderText(string|null $str = null)
Convinient method for plain text output. Sets HTTP header text/plain and echo $str.
at line 297
final static void
renderHtml(string|null $str = null)
Convinient method for html output. Sets HTTP header text/html and echo $str.
at line 306
final static void
renderJson(array|null $json = null)
Convinient method for json output. Sets HTTP header application/json and echo serialized $json.