MilkCore

AbstractController
in package

AbstractYes

Abstract Controller Class

This class serves as the base for module management in the framework. It provides a standardized structure for initializing modules, handling routing, managing permissions, and interacting with models. Controllers that extend this class gain access to automated installation, update, and uninstallation functionality, as well as CLI command registration.

Tags
example
class PostsController extends \MilkCore\AbstractController {
    protected $page = 'posts';
    protected $title = 'Posts';
    protected $access = 'authorized';
    protected $menu_links = [
        ['url'=> '', 'name'=> 'Posts', 'icon'=> '', 'group'=> 'posts']
    ];

    public function bootstrap() {
        $this->model = new PostsModel();
        $this->router = new PostsRouter();
    }
}
new PostsController();

Table of Contents

Methods

__construct()  : mixed
Constructor
_api_init()  : mixed
_bootstrap()  : mixed
_cli_init()  : mixed
Hook initialization method for CLI
_hook_init()  : mixed
_install_check_data()  : mixed
_install_done()  : mixed
_install_execute()  : mixed
Questo serve solo a caricare il bootstrap della classe se non è già stato caricato.
_install_get_html_modules()  : mixed
_install_update()  : mixed
_jobs_init()  : mixed
_jobs_start()  : mixed
access()  : bool
Check if the current user has access to the module
api_init()  : mixed
Hook initialization method for API
bootstrap()  : mixed
Bootstrap method
cli_init()  : mixed
Hook initialization method for CLI
get_model()  : mixed
get_module_name()  : string
Get module name from class name
get_page()  : mixed
get_title()  : mixed
hook_init()  : mixed
Hook initialization method
init()  : mixed
Initialize the module for page rendering
install_check_data()  : array<string|int, mixed>
Validate installation data
install_done()  : string
Installation completion message
install_execute()  : void
Execute the module installation
install_get_html_modules()  : string
Get HTML for the installation page
install_update()  : string
Update the module
jobs_init()  : mixed
Hook initialization method for background jobs
jobs_start()  : mixed
Hook method for job execution start
setup_cli_hooks()  : mixed
Set up CLI hooks
shell_install_module()  : mixed
Install the module
shell_uninstall_module()  : mixed
Uninstall the module
shell_update_module()  : mixed
Update the module

Methods

__construct()

Constructor

public __construct() : mixed

Initializes the module, registers hooks, and sets permissions for authorized modules. If the current page matches this module's page or if running in CLI mode, the init() method is called.

_cli_init()

Hook initialization method for CLI

public _cli_init() : mixed

This method is called during the 'cli-init' hook phase. It can be overridden in child classes to perform actions when the system is initialized and all modules are loaded in CLI context.

_install_check_data()

public _install_check_data(mixed $errors, mixed $data) : mixed
Parameters
$errors : mixed
$data : mixed

_install_execute()

Questo serve solo a caricare il bootstrap della classe se non è già stato caricato.

public _install_execute([mixed $data = [] ]) : mixed
Parameters
$data : mixed = []

_install_get_html_modules()

public _install_get_html_modules(string $html, mixed $errors) : mixed
Parameters
$html : string
$errors : mixed

access()

Check if the current user has access to the module

public access() : bool

Verifies permissions based on the module's access level setting.

Return values
bool

True if the user has access, false otherwise

api_init()

Hook initialization method for API

public api_init() : mixed

This method is called during the 'api-init' hook phase. It can be overridden in child classes to perform actions when the system is initialized and all modules are loaded in API context.

bootstrap()

Bootstrap method

public bootstrap() : mixed

This method is called during the 'init', 'jobs-init', 'cli-init' and 'api-init' hook phases. Since controllers are loaded at system startup, to avoid loading all other classes required by the module, it's better to avoid initializing them here when possible. It's not necessary to include class files via require because the system uses lazy loading to load classes when they are needed.

Override this method in child classes to initialize module-specific components like models and routers that should be available across different contexts.

Tags
example
public function bootstrap() {
    $this->model = new PostsModel();
    $this->router = new PostsRouter();
}

cli_init()

Hook initialization method for CLI

public cli_init() : mixed

This method is called during the 'cli-init' hook phase. It can be overridden in child classes to perform actions when the system is initialized and all modules are loaded in CLI context.

get_module_name()

Get module name from class name

public get_module_name() : string

Extracts the module name from the controller class name by removing 'Controller' and converting to lowercase.

Return values
string

The module name

hook_init()

Hook initialization method

public hook_init() : mixed

This method is called during the 'init' hook phase. It can be overridden in child classes to perform actions when the system is initialized and all modules are loaded.

init()

Initialize the module for page rendering

public init() : mixed

This method is called only when the current page matches this module's page. It can be used to load JavaScript and CSS files specific to the module. The parent::init() method starts the bootstrap process and initializes the router if present, ensuring the same model instance is available in the router.

Override this method in child classes to add module-specific assets and perform page-specific initialization tasks.

Tags
example
public function init() {
    Theme::set('javascript', Route::url().'/modules/my_module/assets/my_module.js');
    Theme::set('styles', Route::url().'/modules/my_module/assets/my_module.css');
    parent::init();
}

install_check_data()

Validate installation data

public install_check_data(array<string|int, mixed> $errors[, array<string|int, mixed> $data = [] ]) : array<string|int, mixed>

Performs validation on the data submitted in the installation form.

Parameters
$errors : array<string|int, mixed>

Current validation errors

$data : array<string|int, mixed> = []

Data from the installation form

Tags
example
public function install_check_data($errors, array $data = []) {
    if ($data['my_setting'] == '') {
        $errors['my_setting'] = 'My setting is required';
    }
    return $errors;
}
Return values
array<string|int, mixed>

Updated validation errors

install_done()

Installation completion message

public install_done(string $html) : string

This method is called after installation and allows modifying the HTML of the installation completion page.

Parameters
$html : string

Current HTML of the completion page

Tags
example
public function install_done($html) {
    $html .= '<div>Module installed correctly</div>';
    return $html;
}
Return values
string

Modified HTML

install_execute()

Execute the module installation

public install_execute([array<string|int, mixed> $data = [] ]) : void

Performs the actual installation of the module, typically by creating database tables through the model's build_table method.

Parameters
$data : array<string|int, mixed> = []

Data from the installation form

Tags
example
public function install_execute($data = []) {
    // Install table
    $this->model->build_table();
    // Save other data from the form
    Config::set('my_setting', $data['my_setting']);
}

install_get_html_modules()

Get HTML for the installation page

public install_get_html_modules(string $html, array<string|int, mixed> $errors) : string

Allows modifying the installation page with custom form elements. This method is called if the system is not installed or there's a new version.

Parameters
$html : string

The current HTML of the installation page

$errors : array<string|int, mixed>

Any validation errors from the installation form

Return values
string

The modified HTML

install_update()

Update the module

public install_update(string $html) : string

This method is called when updating the module. It typically rebuilds the database tables to incorporate any schema changes.

Parameters
$html : string

Current HTML of the update page

Return values
string

Modified HTML

jobs_init()

Hook initialization method for background jobs

public jobs_init() : mixed

This method is called during the 'jobs-init' hook phase. It can be overridden in child classes to perform actions when the system is initialized and all modules are loaded in the background jobs context.

jobs_start()

Hook method for job execution start

public jobs_start() : mixed

This method is called during the 'jobs-start' hook phase. It can be overridden in child classes to perform actions when background jobs are about to start executing.

setup_cli_hooks()

Set up CLI hooks

public setup_cli_hooks() : mixed

Registers shell commands for installing, updating, and uninstalling the module if the model has the necessary methods. Also automatically registers any method that starts with 'shell_' as a CLI command.

Tags
example
public function setup_cli_hooks() {
    parent::setup_cli_hooks();
    // Add another command to the shell
    Cli::set($this->page.":my_command", [$this, 'my_command']);
}

public function my_command($param1, $param2) {
    Cli::echo("My command called with params ".$param1." ".$param2);
}

shell_install_module()

Install the module

public shell_install_module() : mixed

This shell command installs the module by calling the install_execute method. It can be run via CLI or by an admin user.

Tags
example
php cli.php posts:install

shell_uninstall_module()

Uninstall the module

public shell_uninstall_module() : mixed

This shell command uninstalls the module by dropping its tables and disabling the module directory. It can be run via CLI or by an admin user.

Tags
example
php cli.php posts:uninstall

shell_update_module()

Update the module

public shell_update_module() : mixed

This shell command updates the module by calling the install_update method. It can be run via CLI or by an admin user.

Tags
example
php cli.php posts:update

        
On this page

Search results