🛠️ 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.
$modx = modx();🧾 query(string $class)
Quickly creates a query for a model’s table.
query(modResource::class);
// or
pbQuery::table(modResource::class);⚙️ config(string $key, $default = null)
Retrieves a value from the configuration.
$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.
$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.
$uri = uri();
// for example: /about.html → "about"
// /blog/post/ → "blog/post"🏷️ alias()
Returns the current page alias (the last part of the URI).
$alias = alias();
// например: /blog/post → "post"
// /about → "about"💾 cache(string $key, $value = null, int $ttl = 3600)
Gets or sets a cache value.
$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.
$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.
$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.
view('components.card', ['title' => 'Dashboard']);
// or
View::make('components.card', ['title' => 'Dashboard']);🔁 redirect(...$args)
Creates a redirect response.
$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.
$url = route('post.show', ['id' => 10]);
// or
Route::route('post.show', ['id' => 10]);🌍 lang(string $key, array $replace = [], string $locale = '')
Retrieves a translated string.
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.
if (auth()) {
echo 'You are authenticated';
}
// or
if (auth('en')) {
echo 'Authenticated in "en" context';
}🔁 Equivalent to:
$modx->user && $modx->user->isAuthenticated($context ?: $modx->context->key);❌ abort(int $code = 404, string $text = '')
Aborts execution and returns an error response.
abort(403, 'Access denied');
// or
response()->abort(403, 'Access denied')->send();📄 logger($message)
Logs an error message.
logger('An error occurred while saving data');
// or
Log::error('An error occurred while saving data');