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

__construct($core, $elakey)

Each module has to be initialized with eladmin instance and its own elakey.

final
core()

Return core instance

final
string
elakey()

Each module has its elakey - index in modules array - used to address requests.

final
bool
auth(string|null $action = null)

Check if user is authorized to do action, or athorized to access module at all.

string
title()

Get name of the module.

string
icon()

Get icon of the module.

final
string
requestUrl(string|null $action = null, array $args = [])

Return url for this module.

final
string
assetUrl(string $path, string|null $version = null)

Create asset url, file path relative to /assets directory. Default $version = eladmin version

void
prepare()

Runs before any action is executed.

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.

final
string
render(string $template, array $args = [])

Return rendered view.

void
renderAsset(string $path)

Determinate asset content-type and render it.

final static
array
actions()

Return array of all defined actions.

final static
bool
hasAction(string $action)

Check if actions is defined.

final static
bool
isAction(string $action)

Check if actions is executed.

final static
string
parseAction(string $action)

We want action keys to be case insensitive. This converts action to lowercase.

final static
void
renderText(string|null $str = null)

Convinient method for plain text output. Sets HTTP header text/plain and echo $str.

final static
void
renderHtml(string|null $str = null)

Convinient method for html output. Sets HTTP header text/html and echo $str.

final static
void
renderJson(array|null $json = null)

Convinient method for json output. Sets HTTP header application/json and echo serialized $json.

Protected Methods

array
views()

Extends array of directories of views and assets.

Blade
blade()

Return Blade instance

Private Methods

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.

Details

at line 96
__construct($core, $elakey)

Each module has to be initialized with eladmin instance and its own elakey.

Parameters

$core
$elakey

at line 104
final Core core()

Return core instance

Return Value

Core

at line 111
final string elakey()

Each module has its elakey - index in modules array - used to address requests.

Return Value

string

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.

Parameters

string|null $action

Return Value

bool

at line 133
string title()

Get name of the module.

Return Value

string

at line 140
string icon()

Get icon of the module.

Return Value

string

at line 147
final string requestUrl(string|null $action = null, array $args = [])

Return url for this module.

Parameters

string|null $action
array $args

Return Value

string

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

Parameters

string $path
string|null $version

Return Value

string

at line 167
void prepare()

Runs before any action is executed.

Return Value

void

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.

Parameters

string|null $action

Return Value

array|null

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.

Parameters

array|null $roles
string|null $action

Return Value

void

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());
}

Return Value

array

at line 228
protected Blade blade()

Return Blade instance

Return Value

Blade

at line 235
final string render(string $template, array $args = [])

Return rendered view.

Parameters

string $template
array $args

Return Value

string

at line 243
void renderAsset(string $path)

Determinate asset content-type and render it.

Parameters

string $path

Return Value

void

at line 250
final static array actions()

Return array of all defined actions.

Return Value

array

at line 267
final static bool hasAction(string $action)

Check if actions is defined.

Parameters

string $action

Return Value

bool

at line 274
final static bool isAction(string $action)

Check if actions is executed.

Parameters

string $action

Return Value

bool

at line 281
final static string parseAction(string $action)

We want action keys to be case insensitive. This converts action to lowercase.

Parameters

string $action

Return Value

string

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.

Parameters

string|null $str

Return Value

void

at line 297
final static void renderHtml(string|null $str = null)

Convinient method for html output. Sets HTTP header text/html and echo $str.

Parameters

string|null $str

Return Value

void

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.

Parameters

array|null $json

Return Value

void