Global Helpers
ApnaPHP provides a comprehensive set of global helper functions that make development faster and more convenient. These functions are available throughout your application without requiring any imports.
Response Helpers
notFound()
Trigger a 404 response programmatically.
notFound(string $message = 'Page not found', int $status = 404): Response
Example:
if (!$user) {
return notFound('User not found');
}
redirect()
Create a redirect response.
redirect(string $url, int $status = 302): Response
Example:
return redirect('/dashboard');
return redirect('/login', 301);
json()
Create a JSON response.
json(mixed $data, int $status = 200): Response
Example:
return json(['message' => 'Success', 'data' => $user]);
return json(['error' => 'Not found'], 404);
html()
Create an HTML response.
html(string $content, int $status = 200): Response
Example:
return html('<h1>Hello World</h1>');
abort()
Abort the request with a specific status code.
abort(int $status, string $message = ''): Response
Example:
return abort(403, 'Access denied');
return abort(500);
redirectBack()
Redirect back to previous page.
redirectBack(int $status = 302): Response
Example:
return redirectBack();
Error Response Helpers
serverError()
Trigger a 500 response programmatically.
serverError(string $message = 'Internal Server Error', int $status = 500): Response
maintenanceMode()
Trigger a 503 maintenance mode response.
maintenanceMode(string $message = 'Site is under maintenance', int $status = 503): Response
customNotFound()
Create a custom 404 response with data.
customNotFound(array $data = []): Response
customServerError()
Create a custom 500 response with data.
customServerError(array $data = []): Response
customMaintenanceMode()
Create a custom 503 maintenance response with data.
customMaintenanceMode(array $data = []): Response
Environment Helpers
env()
Get environment variable value.
env(string $key, mixed $default = null): mixed
Example:
$dbHost = env('DB_HOST', 'localhost');
$debug = env('APP_DEBUG', false);
Metadata Helpers
metadata()
Create and set metadata automatically.
metadata(array $data = []): \ApnaPHP\Support\Metadata
Example:
metadata([
'title' => 'My Page',
'description' => 'Page description',
'keywords' => 'php, framework, apnaphp'
]);
setMetadata()
Set page metadata.
setMetadata(\ApnaPHP\Support\Metadata $metadata): void
getMetadata()
Get current page metadata.
getMetadata(): ?\ApnaPHP\Support\Metadata
mergeMetadata()
Merge metadata with existing.
mergeMetadata(array $data): void
title()
Set page title.
title(string $title): void
Example:
title('Welcome to ApnaPHP');
description()
Set page description.
description(string $description): void
keywords()
Set page keywords.
keywords(string $keywords): void
canonical()
Set canonical URL.
canonical(string $url): void
openGraph()
Set Open Graph data.
openGraph(array $data): void
Example:
openGraph([
'title' => 'My Page',
'description' => 'Page description',
'image' => 'https://example.com/image.jpg'
]);
twitter()
Set Twitter card data.
twitter(array $data): void
meta()
Add custom meta tag.
meta(string $name, string $content, string $property = null): void
structuredData()
Add structured data (JSON-LD).
structuredData(array $data): void
File Upload Helpers
fileUpload()
Create file upload helper instance.
fileUpload(array $file, string $uploadPath = 'uploads'): \ApnaPHP\Support\FileUpload
Example:
$uploadHelper = fileUpload($_FILES['avatar'], 'avatars');
$path = $uploadHelper->upload();
downloadFile()
Create file download response.
downloadFile(string $filePath, string $filename = null, array $headers = []): Response
serveFile()
Create file serving response (inline display).
serveFile(string $filePath, string $filename = null, array $headers = []): Response
Validation Helpers
validate()
Create validation instance.
validate(array $data = []): \ApnaPHP\Support\Validation
Example:
$validator = validate($_POST, [
'name' => 'required|min:3',
'email' => 'required|email'
]);
if ($validator->fails()) {
$errors = $validator->errors();
}
validateRequest()
Validate request data with rules.
validateRequest(array $rules, array $messages = []): \ApnaPHP\Support\Validation
validateField()
Validate a single field.
validateField(string $field, mixed $value, string $rules, array $messages = []): \ApnaPHP\Support\Validation
Validation Utility Functions
isValidEmail()
Check if email is valid.
isValidEmail(string $email): bool
isValidUrl()
Check if URL is valid.
isValidUrl(string $url): bool
isValidIp()
Check if IP is valid.
isValidIp(string $ip): bool
isValidPhone()
Check if phone number is valid.
isValidPhone(string $phone): bool
isValidCreditCard()
Check if credit card is valid using Luhn algorithm.
isValidCreditCard(string $cardNumber): bool
isValidUuid()
Check if UUID is valid.
isValidUuid(string $uuid): bool
Sanitization Helpers
sanitizeInput()
Sanitize input data.
sanitizeInput(mixed $input): mixed
escapeHtml()
Escape HTML content.
escapeHtml(string $content): string
cleanString()
Clean string (remove special characters, trim, etc.).
cleanString(string $string): string
Authentication Helpers
auth()
Get authentication instance.
auth(): \ApnaPHP\Support\Auth
user()
Get current authenticated user.
user(): ?array
userId()
Get current user ID.
userId(): ?int
userEmail()
Get current user email.
userEmail(): ?string
userName()
Get current user name.
userName(): ?string
userRole()
Get current user role.
userRole(): ?string
isLoggedIn()
Check if user is logged in.
isLoggedIn(): bool
isGuest()
Check if user is guest (not logged in).
isGuest(): bool
isAdmin()
Check if current user is admin.
isAdmin(): bool
hasRole()
Check if current user has specific role.
hasRole(string $role): bool
requireAuth()
Require user to be authenticated.
requireAuth(): void
requireRole()
Require user to have specific role.
requireRole(string $role): void
requireAdmin()
Require user to be admin.
requireAdmin(): void
login()
Attempt to login user.
login(array $credentials, bool $remember = false): bool
logout()
Logout current user.
logout(): void
register()
Register new user.
register(array $data): array
hashPassword()
Hash password.
hashPassword(string $password): string
verifyPassword()
Verify password against hash.
verifyPassword(string $password, string $hash): bool
changePassword()
Change user password.
changePassword(string $currentPassword, string $newPassword): bool
generatePasswordResetToken()
Generate password reset token.
generatePasswordResetToken(string $email): ?string
resetPassword()
Reset password with token.
resetPassword(string $token, string $newPassword): bool
Session & Flash Helpers
csrfToken()
Generate CSRF token.
csrfToken(): string
verifyCsrfToken()
Verify CSRF token.
verifyCsrfToken(string $token): bool
csrfField()
Generate CSRF hidden input field.
csrfField(): string
old()
Get old input value (for form repopulation).
old(string $key, mixed $default = ''): mixed
flash()
Set flash message.
flash(string $key, mixed $value): void
getFlash()
Get and remove flash message.
getFlash(string $key, mixed $default = null): mixed
hasFlash()
Check if flash message exists.
hasFlash(string $key): bool
Console Logging Functions
console_log()
Log message to console (alias for Console::log).
console_log(string $level, string $message, array $context = []): void
console_error()
Log error message to console.
console_error(string $message, array $context = []): void
console_warning()
Log warning message to console.
console_warning(string $message, array $context = []): void
console_success()
Log success message to console.
console_success(string $message, array $context = []): void
console_info()
Log info message to console.
console_info(string $message, array $context = []): void
console_debug()
Log debug message to console.
console_debug(string $message, array $context = []): void
console_dump()
Dump variable to console.
console_dump(mixed $var, string $label = ''): void
console_time()
Log execution time to console.
console_time(string $label = 'Execution'): void
console_memory()
Log memory usage to console.
console_memory(string $label = 'Memory'): void
console_query()
Log SQL query to console.
console_query(string $sql, array $bindings = []): void
console_request()
Log HTTP request to console.
console_request(string $method, string $url, array $data = []): void
console_response()
Log HTTP response to console.
console_response(int $status, mixed $data = null): void
console_file()
Log file operation to console.
console_file(string $operation, string $path, bool $success = true): void
console_database()
Log database operation to console.
console_database(string $operation, string $table, int $affected = 0): void
console_cache()
Log cache operation to console.
console_cache(string $operation, string $key, bool $success = true): void
console_security()
Log security event to console.
console_security(string $event, string $details = ''): void
console_performance()
Log performance metrics to console.
console_performance(string $metric, float $value, string $unit = 'ms'): void
String Utility Functions
camelCase()
Convert string to camelCase.
camelCase(string $string): string
Example:
echo camelCase('hello_world'); // helloWorld
echo camelCase('Hello-World'); // helloWorld
studlyCase()
Convert string to StudlyCase (PascalCase).
studlyCase(string $string): string
Example:
echo studlyCase('hello_world'); // HelloWorld
echo studlyCase('hello-world'); // HelloWorld
snakeCase()
Convert string to snake_case.
snakeCase(string $string): string
Example:
echo snakeCase('HelloWorld'); // hello_world
echo snakeCase('helloWorld'); // hello_world
kebabCase()
Convert string to kebab-case.
kebabCase(string $string): string
Example:
echo kebabCase('HelloWorld'); // hello-world
echo kebabCase('helloWorld'); // hello-world
randomString()
Generate random string.
randomString(int $length = 16, string $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): string
Example:
$token = randomString(32); // Generate 32 character random string
$code = randomString(6, '0123456789'); // Generate 6 digit code
uuid()
Generate UUID v4.
uuid(): string
Example:
$id = uuid(); // e.g., "550e8400-e29b-41d4-a716-446655440000"
startsWith()
Check if string starts with given substring.
startsWith(string $haystack, string $needle): bool
Example:
if (startsWith($email, 'admin')) {
// Email starts with 'admin'
}
endsWith()
Check if string ends with given substring.
endsWith(string $haystack, string $needle): bool
Example:
if (endsWith($filename, '.pdf')) {
// Filename ends with '.pdf'
}
contains()
Check if string contains given substring.
contains(string $haystack, string $needle): bool
Example:
if (contains($text, 'keyword')) {
// Text contains 'keyword'
}
limit()
Limit string length.
limit(string $string, int $limit = 100, string $end = '...'): string
Example:
$short = limit($longText, 50); // Limit to 50 characters
$preview = limit($article, 200, '... [read more]');
slug()
Generate slug from string.
slug(string $string, string $separator = '-'): string
Example:
$slug = slug('Hello World PHP'); // hello-world-php
$slug = slug('Product Name 123', '_'); // product_name_123
Array Utility Functions
arrayGet()
Get array value using dot notation.
arrayGet(array $array, string $key, mixed $default = null): mixed
Example:
$config = [
'database' => [
'host' => 'localhost',
'port' => 3306
]
];
$host = arrayGet($config, 'database.host'); // localhost
$timeout = arrayGet($config, 'database.timeout', 30); // 30 (default)
arraySet()
Set array value using dot notation.
arraySet(array &$array, string $key, mixed $value): void
Example:
$config = [];
arraySet($config, 'database.host', 'localhost');
// $config = ['database' => ['host' => 'localhost']]
arrayHas()
Check if array has key using dot notation.
arrayHas(array $array, string $key): bool
Example:
if (arrayHas($config, 'database.host')) {
// Key exists
}
arrayRemove()
Remove array value using dot notation.
arrayRemove(array &$array, string $key): void
Example:
arrayRemove($config, 'database.password');
arrayFlatten()
Flatten array with dot notation.
arrayFlatten(array $array, string $prefix = ''): array
Example:
$nested = [
'database' => [
'host' => 'localhost',
'port' => 3306
]
];
$flat = arrayFlatten($nested);
// ['database.host' => 'localhost', 'database.port' => 3306]
Formatting Functions
formatBytes()
Convert bytes to human readable format.
formatBytes(int $bytes, int $precision = 2): string
Example:
echo formatBytes(1024); // 1.00 KB
echo formatBytes(1048576); // 1.00 MB
echo formatBytes(1073741824); // 1.00 GB
formatNumber()
Format number with thousands separator.
formatNumber(int|float $number, int $decimals = 0, string $decimalSeparator = '.', string $thousandsSeparator = ','): string
Example:
echo formatNumber(1234567); // 1,234,567
echo formatNumber(1234.56, 2); // 1,234.56
echo formatNumber(1234.56, 2, ',', '.'); // 1.234,56
formatCurrency()
Format currency.
formatCurrency(float $amount, string $currency = 'USD', string $locale = 'en_US'): string
Example:
echo formatCurrency(1234.56); // $1,234.56
echo formatCurrency(1234.56, 'EUR', 'de_DE'); // 1.234,56 €
echo formatCurrency(1234.56, 'INR', 'en_IN'); // ₹1,234.56
formatDate()
Format date.
formatDate(string|int $date, string $format = 'Y-m-d H:i:s'): string
Example:
echo formatDate('2024-01-15'); // 2024-01-15 00:00:00
echo formatDate('2024-01-15', 'd M Y'); // 15 Jan 2024
echo formatDate(time(), 'F j, Y'); // January 15, 2024
timeAgo()
Get relative time (e.g., "2 hours ago").
timeAgo(string|int $date): string
Example:
echo timeAgo('2024-01-15 10:00:00'); // 2 hours ago
echo timeAgo(time() - 3600); // 1 hour ago
echo timeAgo(time() - 86400); // 1 day ago
File Utility Functions
fileExtension()
Get file extension.
fileExtension(string $filename): string
Example:
echo fileExtension('document.pdf'); // pdf
echo fileExtension('image.PNG'); // png (lowercase)
fileName()
Get file name without extension.
fileName(string $filename): string
Example:
echo fileName('document.pdf'); // document
echo fileName('/path/to/file.txt'); // file
isImage()
Check if file is image.
isImage(string $filename): bool
Example:
if (isImage('photo.jpg')) {
// File is an image
}
fileSize()
Get file size.
fileSize(string $filename): int
Example:
$size = fileSize('/path/to/file.pdf'); // Size in bytes
ensureDirectory()
Create directory if it doesn't exist.
ensureDirectory(string $path, int $permissions = 0755): bool
Example:
ensureDirectory('storage/uploads/avatars');
ensureDirectory('logs', 0777);
deleteDirectory()
Delete directory recursively.
deleteDirectory(string $path): bool
Example:
deleteDirectory('temp/cache');
copyDirectory()
Copy directory recursively.
copyDirectory(string $source, string $destination): bool
Example:
copyDirectory('source/folder', 'destination/folder');
Other Utility Functions
e()
Sanitize string for HTML output.
e(string $string): string
Example:
echo e('<script>alert("xss")</script>');
// <script>alert("xss")</script>
class_basename()
Get the class basename from a fully qualified class name.
class_basename(string|object $class): string
Example:
$basename = class_basename('App\Models\User'); // Returns 'User'
$basename = class_basename($user); // Returns 'User'
classExists()
Check if class exists.
classExists(string $class): bool
Example:
if (classExists('App\Models\User')) {
// Class exists
}
methodExists()
Check if method exists.
methodExists(string|object $class, string $method): bool
Example:
if (methodExists($user, 'save')) {
// Method exists
}
Usage Examples
Basic Response Handling
<?php
// app/api/users/route.apna.php
function GET(Request $request): Response
{
$users = User::all();
if (empty($users)) {
return notFound('No users found');
}
return json(['users' => $users]);
}
function POST(Request $request): Response
{
try {
$validated = validate($request->all(), [
'name' => 'required|min:3',
'email' => 'required|email|unique:users,email'
]);
$user = User::create($validated);
return json(['user' => $user], 201);
} catch (ValidationException $e) {
return json(['errors' => $e->errors()], 422);
}
}
Authentication Middleware
<?php
// app/admin/middleware.apna.php
if (!auth()->check()) {
return redirect('/login')->withError('Please login to continue.');
}
if (!auth()->user()['is_admin']) {
return abort(403, 'Admin access required.');
}
File Upload Handling
<?php
// app/upload/page.apna.php
function POST(Request $request): Response
{
if (!$request->file('avatar')) {
return json(['error' => 'No file uploaded'], 400);
}
$uploadHelper = fileUpload($request->file('avatar'), 'avatars');
$errors = $uploadHelper->validate([
'types' => ['image/jpeg', 'image/png', 'image/gif'],
'max_size' => '2MB'
]);
if (!empty($errors)) {
return json(['errors' => $errors], 422);
}
$path = $uploadHelper->upload();
return json(['message' => 'Upload successful', 'path' => $path]);
}
Metadata Management
<?php
// app/about/page.apna.php
metadata([
'title' => 'About Us - ApnaPHP',
'description' => 'Learn more about the ApnaPHP framework',
'keywords' => 'php, framework, about, apnaphp',
'canonical' => 'https://apnaphp.com/about'
]);
openGraph([
'title' => 'About Us',
'description' => 'Learn more about the ApnaPHP framework',
'image' => 'https://apnaphp.com/images/about.jpg'
]);
?>
<h1>About ApnaPHP</h1>
<p>Welcome to our about page!</p>
String Manipulation Example
<?php
// Generate slug from title
$title = "Hello World! This is ApnaPHP";
$slug = slug($title); // hello-world-this-is-apnaphp
// Convert to different cases
$camel = camelCase('hello_world_php'); // helloWorldPhp
$studly = studlyCase('hello-world-php'); // HelloWorldPhp
$snake = snakeCase('HelloWorldPHP'); // hello_world_p_h_p
$kebab = kebabCase('HelloWorldPHP'); // hello-world-p-h-p
// Generate random tokens
$token = randomString(32); // For API tokens
$uuid = uuid(); // For unique IDs
// String checks
if (startsWith($email, 'admin@')) {
// Admin email
}
if (endsWith($filename, '.pdf')) {
// PDF file
}
if (contains($text, 'keyword')) {
// Keyword found
}
// Limit text length
$preview = limit($longText, 100, '... [Read more]');
Array Manipulation Example
<?php
// Working with nested arrays using dot notation
$config = [
'app' => [
'name' => 'My App',
'debug' => true
],
'database' => [
'host' => 'localhost',
'port' => 3306
]
];
// Get nested value
$appName = arrayGet($config, 'app.name'); // My App
$timeout = arrayGet($config, 'database.timeout', 30); // 30 (default)
// Set nested value
arraySet($config, 'database.username', 'root');
arraySet($config, 'cache.driver', 'redis');
// Check if key exists
if (arrayHas($config, 'app.debug')) {
// Key exists
}
// Remove nested key
arrayRemove($config, 'database.password');
// Flatten array
$flat = arrayFlatten($config);
// ['app.name' => 'My App', 'app.debug' => true, 'database.host' => 'localhost', ...]
Formatting Example
<?php
// Format bytes
echo formatBytes(1024); // 1.00 KB
echo formatBytes(1048576); // 1.00 MB
echo formatBytes(5368709120); // 5.00 GB
// Format numbers
echo formatNumber(1234567); // 1,234,567
echo formatNumber(1234.5678, 2); // 1,234.57
// Format currency
echo formatCurrency(1234.56); // $1,234.56
echo formatCurrency(1234.56, 'EUR', 'de_DE'); // 1.234,56 €
echo formatCurrency(1234.56, 'INR', 'en_IN'); // ₹1,234.56
// Format dates
echo formatDate('2024-01-15', 'd M Y'); // 15 Jan 2024
echo formatDate(time(), 'F j, Y'); // January 15, 2024
// Relative time
echo timeAgo(time() - 3600); // 1 hour ago
echo timeAgo('2024-01-01 10:00:00'); // X days ago
File Utilities Example
<?php
// Working with files
$filename = 'document.pdf';
$extension = fileExtension($filename); // pdf
$name = fileName($filename); // document
if (isImage('photo.jpg')) {
// It's an image file
}
// Get file size
$size = fileSize('/path/to/file.pdf');
$humanSize = formatBytes($size); // 2.50 MB
// Directory operations
ensureDirectory('storage/uploads/avatars'); // Create if not exists
copyDirectory('source/folder', 'backup/folder'); // Backup
deleteDirectory('temp/cache'); // Clean up
Complete API Example
<?php
// app/api/blog/[slug]/route.apna.php
use ApnaPHP\Routing\Request;
use ApnaPHP\Routing\Response;
function GET(Request $request): Response
{
$slug = $request->param('slug');
// Find post by slug
$post = Post::where('slug', $slug)->first();
if (!$post) {
return notFound('Post not found');
}
// Track view
console_info('Post viewed', ['slug' => $slug, 'id' => $post['id']]);
// Format response
$response = [
'id' => $post['id'],
'title' => $post['title'],
'slug' => $post['slug'],
'preview' => limit($post['content'], 200),
'author' => $post['author'],
'published_at' => formatDate($post['published_at'], 'F j, Y'),
'published_ago' => timeAgo($post['published_at']),
'reading_time' => ceil(str_word_count($post['content']) / 200) . ' min read'
];
return json($response);
}
function PUT(Request $request): Response
{
// Check authentication
if (!auth()->check()) {
return json(['error' => 'Unauthorized'], 401);
}
if (!auth()->user()->isAdmin()) {
return abort(403, 'Admin access required');
}
// Validate input
try {
$validated = validate($request->all(), [
'title' => 'required|min:3|max:255',
'content' => 'required|min:10'
]);
// Generate slug from title
$validated['slug'] = slug($validated['title']);
// Update post
$slug = $request->param('slug');
$post = Post::where('slug', $slug)->first();
if (!$post) {
return notFound('Post not found');
}
$post->update($validated);
console_success('Post updated', ['slug' => $slug]);
return json([
'message' => 'Post updated successfully',
'post' => $post
]);
} catch (ValidationException $e) {
return json(['errors' => $e->errors()], 422);
}
}
Complete Page Example with Utilities
<?php
// app/blog/[slug]/page.apna.php
use ApnaPHP\Routing\Request;
$slug = $request->param('slug');
$post = Post::where('slug', $slug)->first();
if (!$post) {
return notFound('Post not found');
}
// Set metadata
metadata([
'title' => $post['title'] . ' - My Blog',
'description' => limit(strip_tags($post['content']), 160),
'keywords' => implode(', ', $post['tags'] ?? [])
]);
openGraph([
'title' => $post['title'],
'description' => limit(strip_tags($post['content']), 200),
'image' => $post['featured_image'] ?? '/images/default-og.jpg',
'type' => 'article',
'article:published_time' => $post['published_at'],
'article:author' => $post['author']
]);
twitter([
'card' => 'summary_large_image',
'title' => $post['title'],
'description' => limit(strip_tags($post['content']), 200),
'image' => $post['featured_image'] ?? '/images/default-twitter.jpg'
]);
// Structured data for SEO
structuredData([
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'headline' => $post['title'],
'description' => limit(strip_tags($post['content']), 200),
'author' => [
'@type' => 'Person',
'name' => $post['author']
],
'datePublished' => $post['published_at'],
'dateModified' => $post['updated_at']
]);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<article>
<h1><?= e($post['title']) ?></h1>
<div class="meta">
<span>By <?= e($post['author']) ?></span>
<span><?= formatDate($post['published_at'], 'F j, Y') ?></span>
<span><?= timeAgo($post['published_at']) ?></span>
<span><?= ceil(str_word_count($post['content']) / 200) ?> min read</span>
</div>
<div class="content">
<?= $post['content'] ?>
</div>
<?php if (!empty($post['tags'])): ?>
<div class="tags">
<?php foreach ($post['tags'] as $tag): ?>
<a href="/blog/tag/<?= slug($tag) ?>"><?= e($tag) ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</article>
</body>
</html>
Summary
These global helpers make ApnaPHP development fast and efficient by providing:
- 150+ Helper Functions - Covering common tasks like string manipulation, array operations, formatting, file handling, validation, authentication, and more
- No Imports Required - All functions are globally available throughout your application
- Consistent API - Similar function signatures and naming conventions
- Framework Integration - Deep integration with ApnaPHP's core features
- Developer Friendly - Intuitive names and comprehensive examples
- Production Ready - Tested and optimized for performance
Whether you're building APIs, web pages, or complex applications, these helpers provide the building blocks you need to develop quickly and efficiently.
