Skip to content

🛠️ Helpers

Helpers are global functions that simplify access to key components of the framework. They are wrappers around core classes and methods commonly used during development.

⚙️ modx()

Returns the global instance of modX.

php
$modx = modx();

🧾 query(string $class)

Quickly creates a query for a model’s table.

php
query(modResource::class); 
// or
pbQuery::table(modResource::class);

⚙️ config(string $key, $default = null)

Retrieves a value from the configuration.

php
$debug = config('app.debug', false);
// or
Config::get('app.debug', false) 
    ?? $modx->getOption('app.debug', null, false);

📁 Configuration files are stored in: core/App/config/

📥 request(string $key = '')

Returns the current HTTP request instance or a specific value.

php
$request = request();            // $request = Request::getInstance()
$name = request('name');         // $request->get('name');
request()->validate([...]);      // $request->validate([...]);

🌐 uri()

Returns the current URI without extensions and leading/trailing slashes.

php
$uri = uri();  
// for example: /about.html → "about"
//              /blog/post/ → "blog/post"

🏷️ alias()

Returns the current page alias (the last part of the URI).

php
$alias = alias();  
// например: /blog/post → "post"
//           /about → "about"

💾 cache(string $key, $value = null, int $ttl = 3600)

Gets or sets a cache value.

php
$data = cache('menu');           // $data = Cache::get('menu');
cache('menu', $items, 600);      // Cache::set('menu', $items, 600);

📤 response($content = '', int $status = 200, array $headers = [])

Creates and returns an HTTP response.

php
$response = response();                      // Equivalent to new Response();
return response('OK');                       // $response->text('OK');
return response(['status' => 'ok'], 200);    // $response->json(['status' => 'ok']);

📌 Automatically detects the type: text, json, or object.

validate(array $data, array $rules, array $messages = [])

Runs data validation.

php
$validator = validate(
    ['email' => 'john@example.com'],
    ['email' => 'required|email']
);

// or
$validator = Validator::make($data, $rules, $messages);

🖼️ view(string $template, array $params = [])

Renders a template with parameters.

php
view('components.card', ['title' => 'Dashboard']);
// or
View::make('components.card', ['title' => 'Dashboard']);

🔁 redirect(...$args)

Creates a redirect response.

php
$redirect = redirect();         // $redirect = Redirect::getInstance();
redirect('/login');             // $redirect->to('/login');
redirect()->back();             // $redirect->back();

🧭 route(string $name, array $params = [])

Generates a URL from a named route.

php
$url = route('post.show', ['id' => 10]); 
// or
Route::route('post.show', ['id' => 10]);

🌍 lang(string $key, array $replace = [], string $locale = '')

Retrieves a translated string.

php
lang('messages.welcome'); 
// or
Lang::get('messages.welcome');

📁 Language files are stored in: core/App/lang/{locale}/

🔐 auth(string $context = '')

Checks if the user is authenticated in the given or current context.

php
if (auth()) {
    echo 'You are authenticated';
}

// or

if (auth('en')) {
    echo 'Authenticated in "en" context';
}

🔁 Equivalent to:

php
$modx->user && $modx->user->isAuthenticated($context ?: $modx->context->key);

abort(int $code = 404, string $text = '')

Aborts execution and returns an error response.

php
abort(403, 'Access denied'); 
// or
response()->abort(403, 'Access denied')->send();

📄 logger($message)

Logs an error message.

php
logger('An error occurred while saving data'); 
// or
 Log::error('An error occurred while saving data');

© PageBlocks 2019-present