Modules in hacks implement the main functionality - page processing, implementation of scheduler tasks and API functions. Within the MVC model, they are most similar to "controllers".
Creating a module
Working with modules is carried out from the "Modules" section in the "Files" menu. The modules files themselves are located in the mod
folder of your hack. The file name with the module can consist of small Latin letters, numbers and a hyphen. You cannot use capital letters and special characters, including underscores.
At the time of module creation, its "standard" function with the recommended name is automatically entered in the file. It is recommended to compose the name of the function from the word hack
, the name of the module and the name of the function itself, separated by an underscore, for example: hack_proxy_list
.
The module is loaded using the mod
function of the main class of the hack:
$core->hack->(name)->mod( $module )
For example, connecting the listing
module in the proxy
hack will look like this:
$core->hack->proxy->mod( 'listing' );
At the moment of connecting the module, you can immediately call any function contained in it. This is convenient for organizing routing and quickly launching modules. The called function must take only one required parameter - $core
.
Connecting a module with a function call:
$core->hack->(name)->mod( $module, $func )
For example, calling the hack_proxy_list
function in the listing
module of the proxy
hack:
$core->hack->proxy->mod( 'listing', 'hack_proxy_list' );
When a module with a function name is called, the result of executing that function is returned.
Use cases
Modules are used in three main tasks:
- Page and section handlers. Used to implement routing, which is described below.
- Periodic tasks for the scheduler in
cron-name.php
files. They are described in the section "Scheduler". - API applications in
api-name.php
files. They are described in the section "API".
Setting up routing
Modules are optimal for organizing routing in an application. In the start.php
file, create a function that will handle the routing. It takes the $core
parameter as input and should call the correct module when needed.
The current path through the site is divided into several parts:
https://network.domain/div/item/subdiv/subitem
These elements are available through $core->app
:
$core->app->div
is the current section on the site.$core->app->item
- the element in the selected section.$core->app->id
- integer value from$core->app->item
.$core->app->subdiv
- subsection of the element.$core->app->subitem
- nested element of a nested section.$core->app->subid
- integer value from$core->app->subitem
.$core->app->action
- the value from the action GET parameter, corresponds to the selected action.$core->app->message
- the value from the message GET parameter, corresponds to the success or error message of the action.
In the function, it is convenient to use the value $core->app->div
, which corresponds to the first item in the current site path. An example implementation of routing looks like this:
function hack_proxy_route( $core ) { switch ( $core->app->div ) { case 'proxy-list': $core->hack->proxy->mod( 'listing', 'hack_proxy_list' ); case 'proxy-check': $core->hack->proxy->mod( 'listing', 'hack_proxy_check' ); case 'proxy-stats': $core->hack->proxy->mod( 'stats’, 'hack_proxy_stats' ); } }
When initializing the hack, this function must be specified in the modules
handler. This is done using the $core->handle( 'modules', $handlename )
function, where $handlename
specifies your function.
An example of hack initialization with routing:
function hack_proxy_init( $core ) { $core->handle( 'modules', 'hack_proxy_route' ); }