laravel-api-helpers maintained by omaralalwi
🌟 Features
- API Version Detection: Determine the API version from the request URL or headers.
- API Version Validation: Check if the current request matches or exceeds a specified API version.
- API Request Identification: Identify if a request is targeting the API.
- Request Helpers: Retrieve client IP, locale, token, content type, headers, and check for secure requests.
- Authentication Helpers: Determine API authentication status and retrieve the authenticated user.
📋 Requirements
- PHP 7.4 or higher
- Laravel 7.x or higher
🛠️ Installation
Install the package via Composer:
composer require omaralalwi/laravel-api-helpers
📦 Usage
The helper functions are globally available throughout your Laravel application. You can call them anywhere—controllers, middleware, or routes or blade files.
📚 API Helper Functions Guide
🔍 . get_api_v()
Extracts the API version from the current request by inspecting the URL (e.g., api/v4) or the Accept-Version header.
$apiVersion = get_api_v();
✅ . is_api_v($version)
Determines if the current request is for a specific API version by comparing the detected version with the provided one.
// Check if the current API version is 3
if (is_api_v(3)) {
echo "This is API version 3. ✅";
} else {
echo "This is not API version 3. ❌";
}
⏫ . api_v_at_least($version)
Checks whether the current API version is at least a specified minimum version.
// Verify that the API version is at least version 4
if (api_v_at_least(4)) {
echo "API version is 4 or higher. 🚀";
} else {
echo "API version is below 4. Please upgrade.";
}
🌐 . is_api_request()
Determines whether the current request is an API request by examining the URL, expected JSON responses, and the presence of specific API headers.
if (is_api_request()) {
echo "This is an API request. 🌐";
} else {
echo "This is not an API request.";
}
📡 . get_client_ip()
Retrieves the client's IP address from the current request.
$clientIp = get_client_ip();
echo "Client IP: " . ($clientIp ?? "unknown");
🌍 . get_request_locale()
Gets the locale from the request's Accept-Language header. If not provided, it defaults to the application's locale.
$locale = get_request_locale();
echo "Request locale: {$locale}";
🔑 . get_request_token()
Returns the bearer token from the current request's authorization header.
$token = get_request_token();
echo $token ? "Token: {$token}" : "No token found.";
📝 . get_request_content_type()
Retrieves the Content-Type header from the current request.
$contentType = get_request_content_type();
echo $contentType ? "Content-Type: {$contentType}" : "No Content-Type provided.";
🔒 . is_secure_request()
Checks whether the current request is made over a secure HTTPS connection.
if (is_secure_request()) {
echo "Secure HTTPS request detected. 🔒";
} else {
echo "This is not a secure request.";
}
📋 . get_request_headers()
Returns all headers from the current request as an associative array.
$headers = get_request_headers();
print_r($headers); // Displays all request headers
🔐 . is_api_authenticated()
Determines whether the API user is authenticated using the api guard.
if (is_api_authenticated()) {
echo "API user is authenticated. 🔐";
} else {
echo "API user is not authenticated.";
}
👤 . get_auth_user()
Retrieves the authenticated user from the default authentication guard.
$user = get_auth_user();
echo $user ? "Authenticated user: {$user->name}" : "No authenticated user.";
👥 . get_api_user()
Returns the authenticated user using the api guard.
$apiUser = get_api_user();
echo $apiUser ? "API User: {$apiUser->name}" : "No API user authenticated.";
Comprehensive Example For All Functions
copy & past following block, then see outputs in log file
$apiHelpers = [
'API Version' => get_api_v(),
'Is API v3?' => is_api_v(3),
'API Version At Least 4?' => api_v_at_least(4),
'Is API Request?' => is_api_request(),
'Client IP' => get_client_ip(),
'Request Locale' => get_request_locale(),
'Request Token' => get_request_token(),
'Request Content-Type' => get_request_content_type(),
'Is Secure Request?' => is_secure_request(),
'Request Headers' => json_encode(get_request_headers()),
'Is API Authenticated?' => is_api_authenticated(),
'Authenticated User' => optional(get_auth_user())->name ?? 'No Authenticated User',
'API Authenticated User' => optional(get_api_user())->name ?? 'No API User Authenticated',
];
\Log::info('API Helper Functions Test:');
foreach ($apiHelpers as $key => $value) {
\Log::info("{$key}: " . (is_array($value) ? json_encode($value) : $value));
}
Changelog
See CHANGELOG for recent changes.
Contributors ✨
Thanks to these wonderful people for contributing to this project! 💖
Want to contribute? Check out the contributing guidelines and submit a pull request! 🚀
Security
If you discover any security-related issues, please email omaralwi2010@gmail.com.
Credits
License
The MIT License (MIT). See LICENSE for more information.
📚 Helpful Open Source Packages & Projects
Packages
-
Lexi Translate simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .
-
Gpdf Open Source HTML to PDF converter for PHP & Laravel Applications, supports Arabic content out-of-the-box and other languages.
-
laravel Taxify Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.
-
laravel API Helpers Laravel API Helpers provides a set of helper of helpful helper functions for API Requests ..
-
laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.
-
laravel Trash Cleaner clean logs and debug files for debugging packages.
-
laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.
-
PHP builders sample php traits to add ability to use builder design patterns with easy in PHP applications.
-
PhpPy - PHP Python Interact with python in PHP applications.
-
Laravel Py - Laravel Python interact with python in Laravel applications.
-
deepseek PHP client robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities .
-
deepseek laravel Laravel wrapper for Deepseek PHP client to seamless deepseek AI API integration with Laravel applications.
-
Qwen PHP client robust and community-driven PHP client library for seamless integration with the Qwen API .
-
Laravel qwen wrapper for qwen PHP client to seamless Alibaba qwen AI API integration with Laravel applications..
Dashboards
-
Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.
-
Kunafa Dashboard Vue A feature-rich Vue.js 3 dashboard template with multi-language support and full RTL/LTR bidirectional layout capabilities.
References
-
Clean Code Summary summarize and notes for books and practices about clean code.
-
SOLID Principles Summary summarize and notes for books about SOLID Principles.