🛠️ Global Helper Functions in Squehub Framework
The Squehub Framework comes with a robust set of globally available helper functions designed to simplify common
development tasks. These helpers are defined in the app/core/Helper.php
file and provide clean,
reusable shortcuts for session management, security (CSRF), URL routing, data formatting, flashing messages, and
HTTP redirection.
app/core/Helper.php
⚠️ Using Helpers with Namespaces
To use the helper functions in your own PHP files (controllers, views, middleware, CLI scripts, etc.), you must import the helper namespace:
<?php
use App\Core\Helper;
// Now you can use csrf_token(), route(), flash(), etc.
📚 Available Helper Functions
startSessionIfNotStarted()
route(string $name, array $params = [])
csrf_token()
validateCsrfToken()
CsrfTokenValidator(): bool
is_post(): bool
maskEmail(string $email): string
maskPhoneNumber(string $phone): string
slugify(string $text): string
flash(string $key, ?string $message = null)
priceFormatter(...): string
url(...$segments): string
redirect()
🔍 Detailed Helper Documentation
startSessionIfNotStarted()
startSessionIfNotStarted();
$_SESSION['user_id'] = 123;
route(string $name, array $params = [])
echo route('profile.show', ['id' => 5]);
// Output: http://localhost/profile/5
csrf_token()
<input type="hidden" name="_token" value="<?= csrf_token() ?>">
validateCsrfToken()
if (is_post()) {
validateCsrfToken();
// Proceed safely
}
CsrfTokenValidator(): bool
if (CsrfTokenValidator()) {
// Valid token
} else {
// Invalid token logic
}
is_post(): bool
if (is_post()) {
// Handle form logic
}
maskEmail(string $email)
echo maskEmail('johndoe@example.com');
// Output: jo******@example.com
maskPhoneNumber(string $phone)
echo maskPhoneNumber('1234567890');
// Output: ********90
slugify(string $text)
echo slugify('Hello World! Welcome.');
// Output: hello-world-welcome
flash(string $key, ?string $message = null)
// Controller
flash('success', 'Registration successful!');
// View
<?php if ($msg = flash('success')): ?>
<div class="alert alert-success"><?= $msg ?></div>
<?php endif; ?>
priceFormatter(...)
echo priceFormatter(1234.5); // 1,234.50
echo priceFormatter(99.99, '$'); // $99.99
echo priceFormatter(5000, '€', false); // 5,000.00 €
url(...$segments)
echo url('products', 'shoes', 'nike');
// Output: http://localhost/products/shoes/nike
redirect()
redirect()->to('/dashboard');
redirect()->back();
🆕 Creating Your Own Helpers
Developers can also create custom utility classes or helper functions inside both the global project-level
project/Utils/
directory and inside each package’s own Utils/
directory.
This allows for flexible and modular utility code organization.
Any PHP files placed inside these Utils
directories are automatically loaded globally by Squehub,
meaning their functions and classes are accessible anywhere in your application without explicit imports or requires.
This applies both to the main project/Utils/
folder and the Utils/
folders inside each installed package.
<?php
namespace Project\Utils;
function myCustomHelper() {
// your logic here
}
📌 Best Practices
- ✅ No need to import global Squehub helpers — they’re available everywhere.
- ✅ Use
csrf_token()
in all mutating forms. - ✅ Use
route()
to avoid hardcoded URLs. - ✅ Use
flash()
for user alerts. - ✅ Use
slugify()
for SEO-friendly URLs.
📝 Summary
Squehub’s helper functions simplify common tasks and enforce consistency across your application. Extend as needed for your project.