|
| 1 | +<p align="center"> |
| 2 | + <h1 align="center">Laravel API Helpers 🚀</h1> |
| 3 | + <p align="center">**Laravel API Helpers** is a collection of helper functions designed to streamline API development with Laravel. It enables you to easily detect API versions, validate requests, and access useful request details—all while keeping your code clean and maintainable. ✨</p> |
| 4 | + |
| 5 | + <p align="center"> |
| 6 | + <a href="https://packagist.org/packages/omaralalwi/laravel-api-helpers"> |
| 7 | + <img src="https://img.shields.io/packagist/v/omaralalwi/laravel-api-helpers" alt="Latest Version"> |
| 8 | + </a> |
| 9 | + <a href="https://php.net"> |
| 10 | + <img src="https://img.shields.io/badge/PHP-7.4%2B-blue" alt="PHP Version"> |
| 11 | + </a> |
| 12 | + <a href="LICENSE.md"> |
| 13 | + <img src="https://img.shields.io/badge/license-MIT-brightgreen" alt="License"> |
| 14 | + </a> |
| 15 | + <a href="https://github.com/omaralalwi/laravel-api-helpers/actions"> |
| 16 | + <img src="https://img.shields.io/github/actions/workflow/status/omaralalwi/laravel-api-helpers/tests.yml" alt="Tests Status"> |
| 17 | + </a> |
| 18 | + </p> |
| 19 | + |
| 20 | +## 🌟 Features |
| 21 | + |
| 22 | +- **API Version Detection:** Determine the API version from the request URL or headers. |
| 23 | +- **API Version Validation:** Check if the current request matches or exceeds a specified API version. |
| 24 | +- **API Request Identification:** Identify if a request is targeting the API. |
| 25 | +- **Request Helpers:** Retrieve client IP, locale, token, content type, headers, and check for secure requests. |
| 26 | +- **Authentication Helpers:** Determine API authentication status and retrieve the authenticated user. |
| 27 | + |
| 28 | +## 📋 Requirements |
| 29 | + |
| 30 | +- PHP 7.4 or higher |
| 31 | +- Laravel 7.x or higher |
| 32 | + |
| 33 | +## 🛠️ Installation |
| 34 | + |
| 35 | +Install the package via Composer: |
| 36 | + |
| 37 | +```bash |
| 38 | +composer require omaralalwi/laravel-api-helpers |
| 39 | +``` |
| 40 | + |
| 41 | +## 📦 Usage |
| 42 | + |
| 43 | +The helper functions are globally available throughout your Laravel application. You can call them anywhere—controllers, middleware, or routes or blade files. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## 📚 API Helper Functions Guide |
| 48 | + |
| 49 | +### 🔍 . `get_api_v()` |
| 50 | + |
| 51 | +Extracts the API version from the current request by inspecting the URL (e.g., `api/v4`) or the `Accept-Version` header. |
| 52 | + |
| 53 | +```php |
| 54 | +$apiVersion = get_api_v(); |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### ✅ . `is_api_v($version)` |
| 60 | + |
| 61 | +Determines if the current request is for a specific API version by comparing the detected version with the provided one. |
| 62 | + |
| 63 | +```php |
| 64 | +// Check if the current API version is 3 |
| 65 | +if (is_api_v(3)) { |
| 66 | + echo "This is API version 3. ✅"; |
| 67 | +} else { |
| 68 | + echo "This is not API version 3. ❌"; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### ⏫ . `api_v_at_least($version)` |
| 75 | + |
| 76 | +Checks whether the current API version is at least a specified minimum version. |
| 77 | + |
| 78 | +```php |
| 79 | +// Verify that the API version is at least version 4 |
| 80 | +if (api_v_at_least(4)) { |
| 81 | + echo "API version is 4 or higher. 🚀"; |
| 82 | +} else { |
| 83 | + echo "API version is below 4. Please upgrade."; |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +### 🌐 . `is_api_request()` |
| 90 | + |
| 91 | +Determines whether the current request is an API request by examining the URL, expected JSON responses, and the presence of specific API headers. |
| 92 | + |
| 93 | +```php |
| 94 | +if (is_api_request()) { |
| 95 | + echo "This is an API request. 🌐"; |
| 96 | +} else { |
| 97 | + echo "This is not an API request."; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +### 📡 . `get_client_ip()` |
| 104 | + |
| 105 | +Retrieves the client's IP address from the current request. |
| 106 | + |
| 107 | +```php |
| 108 | +$clientIp = get_client_ip(); |
| 109 | +echo "Client IP: " . ($clientIp ?? "unknown"); |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### 🌍 . `get_request_locale()` |
| 115 | + |
| 116 | +Gets the locale from the request's `Accept-Language` header. If not provided, it defaults to the application's locale. |
| 117 | + |
| 118 | +```php |
| 119 | +$locale = get_request_locale(); |
| 120 | +echo "Request locale: {$locale}"; |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +### 🔑 . `get_request_token()` |
| 126 | + |
| 127 | +Returns the bearer token from the current request's authorization header. |
| 128 | + |
| 129 | +```php |
| 130 | +$token = get_request_token(); |
| 131 | +echo $token ? "Token: {$token}" : "No token found."; |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### 📝 . `get_request_content_type()` |
| 137 | + |
| 138 | +Retrieves the `Content-Type` header from the current request. |
| 139 | + |
| 140 | +```php |
| 141 | +$contentType = get_request_content_type(); |
| 142 | +echo $contentType ? "Content-Type: {$contentType}" : "No Content-Type provided."; |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +### 🔒 . `is_secure_request()` |
| 148 | + |
| 149 | +Checks whether the current request is made over a secure HTTPS connection. |
| 150 | + |
| 151 | +```php |
| 152 | +if (is_secure_request()) { |
| 153 | + echo "Secure HTTPS request detected. 🔒"; |
| 154 | +} else { |
| 155 | + echo "This is not a secure request."; |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +### 📋 . `get_request_headers()` |
| 162 | + |
| 163 | +Returns all headers from the current request as an associative array. |
| 164 | + |
| 165 | +```php |
| 166 | +$headers = get_request_headers(); |
| 167 | +print_r($headers); // Displays all request headers |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +### 🔐 . `is_api_authenticated()` |
| 173 | + |
| 174 | +Determines whether the API user is authenticated using the `api` guard. |
| 175 | + |
| 176 | +```php |
| 177 | +if (is_api_authenticated()) { |
| 178 | + echo "API user is authenticated. 🔐"; |
| 179 | +} else { |
| 180 | + echo "API user is not authenticated."; |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +### 👤 . `get_auth_user()` |
| 187 | + |
| 188 | +Retrieves the authenticated user from the default authentication guard. |
| 189 | + |
| 190 | +```php |
| 191 | +$user = get_auth_user(); |
| 192 | +echo $user ? "Authenticated user: {$user->name}" : "No authenticated user."; |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +### 👥 . `get_api_user()` |
| 198 | + |
| 199 | +Returns the authenticated user using the `api` guard. |
| 200 | + |
| 201 | +```php |
| 202 | +$apiUser = get_api_user(); |
| 203 | +echo $apiUser ? "API User: {$apiUser->name}" : "No API user authenticated."; |
| 204 | +``` |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +## Changelog |
| 209 | + |
| 210 | +See [CHANGELOG](CHANGELOG.md) for recent changes. |
| 211 | + |
| 212 | +## Contributors ✨ |
| 213 | + |
| 214 | +Thanks to these wonderful people for contributing to this project! 💖 |
| 215 | + |
| 216 | +<table> |
| 217 | + <tr> |
| 218 | + <td align="center"> |
| 219 | + <a href="https://github.com/omaralalwi"> |
| 220 | + <img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="100px;" alt="Omar Al Alwi"/> |
| 221 | + <br /> |
| 222 | + <sub><b>Omar Al Alwi</b></sub> |
| 223 | + </a> |
| 224 | + <br /> |
| 225 | + 🏆 Creator |
| 226 | + </td> |
| 227 | + </tr> |
| 228 | +</table> |
| 229 | + |
| 230 | +Want to contribute? Check out the [contributing guidelines](./CONTRIBUTING.md) and submit a pull request! 🚀 |
| 231 | + |
| 232 | + |
| 233 | +## Security |
| 234 | + |
| 235 | +If you discover any security-related issues, please email `omaralwi2010@gmail.com`. |
| 236 | + |
| 237 | +## Credits |
| 238 | + |
| 239 | +- [Omar Alalwi](https://github.com/omaralalwi) |
| 240 | + |
| 241 | +## License |
| 242 | + |
| 243 | +The MIT License (MIT). See [LICENSE](LICENSE.md) for more information. |
| 244 | + |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | +## 📚 Helpful Open Source Packages & Projects |
| 249 | + |
| 250 | +### Packages |
| 251 | + |
| 252 | +- <a href="https://github.com/omaralalwi/lexi-translate"><img src="https://raw.githubusercontent.com/omaralalwi/lexi-translate/master/public/images/lexi-translate-banner.jpg" width="26" height="26" style="border-radius:13px;" alt="lexi translate" /> Lexi Translate </a> simplify managing translations for multilingual Eloquent models with power of morph relationships and caching . |
| 253 | + |
| 254 | +- <a href="https://github.com/omaralalwi/Gpdf"><img src="https://raw.githubusercontent.com/omaralalwi/Gpdf/master/public/images/gpdf-banner-bg.jpg" width="26" height="26" style="border-radius:13px;" alt="Gpdf" /> Gpdf </a> Open Source HTML to PDF converter for PHP & Laravel Applications, supports Arabic content out-of-the-box and other languages. |
| 255 | + |
| 256 | +- <a href="https://github.com/omaralalwi/laravel-taxify"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-taxify/master/public/images/taxify.jpg" width="26" height="26" style="border-radius:13px;" alt="laravel Taxify" /> **laravel Taxify** </a> Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications. |
| 257 | + |
| 258 | +- <a href="https://github.com/omaralalwi/laravel-api-helpers"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-api-helpers/master/public/images/laravel-api-helpers.jpg" width="26" height="26" style="border-radius:13px;" alt="laravel Taxify" /> **laravel API Helpers** </a> Laravel API Helpers provides a set of helper of helpful helper functions for API Requests .. |
| 259 | + |
| 260 | +- <a href="https://github.com/omaralalwi/laravel-deployer"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-deployer/master/public/images/deployer.jpg" width="26" height="26" style="border-radius:13px;" alt="laravel Deployer" /> **laravel Deployer** </a> Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches. |
| 261 | + |
| 262 | +- <a href="https://github.com/omaralalwi/laravel-trash-cleaner"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-trash-cleaner/master/public/images/laravel-trash-cleaner.jpg" width="26" height="26" style="border-radius:13px;" alt="laravel Trash Cleaner" /> **laravel Trash Cleaner** </a> clean logs and debug files for debugging packages. |
| 263 | + |
| 264 | +- <a href="https://github.com/omaralalwi/laravel-time-craft"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-time-craft/master/public/images/laravel-time-craft.jpg" width="26" height="26" style="border-radius:13px;" alt="laravel Time Craft" /> **laravel Time Craft** </a> simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps. |
| 265 | + |
| 266 | +- <a href="https://github.com/omaralalwi/php-builders"><img src="https://repository-images.githubusercontent.com/917404875/c5fbf4c9-d41f-44c6-afc6-0d66cf7f4c4f" width="26" height="26" style="border-radius:13px;" alt="PHP builders" /> **PHP builders** </a> sample php traits to add ability to use builder design patterns with easy in PHP applications. |
| 267 | + |
| 268 | +- <a href="https://github.com/omaralalwi/php-py"> <img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="26" height="26" style="border-radius:13px;" alt="PhpPy - PHP Python" /> **PhpPy - PHP Python** </a> Interact with python in PHP applications. |
| 269 | + |
| 270 | +- <a href="https://github.com/omaralalwi/laravel-py"><img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="26" height="26" style="border-radius:13px;" alt="Laravel Py - Laravel Python" /> **Laravel Py - Laravel Python** </a> interact with python in Laravel applications. |
| 271 | + |
| 272 | +- <a href="https://github.com/deepseek-php/deepseek-php-client"><img src="https://avatars.githubusercontent.com/u/193405629?s=200&v=4" width="26" height="26" style="border-radius:13px;" alt="Deepseek PHP client" /> **deepseek PHP client** </a> robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities . |
| 273 | + |
| 274 | +- <a href="https://github.com/deepseek-php/deepseek-laravel"><img src="https://github.com/deepseek-php/deepseek-laravel/blob/master/public/images/laravel%20deepseek%20ai%20banner.jpg?raw=true" width="26" height="26" style="border-radius:13px;" alt="deepseek laravel" /> **deepseek laravel** </a> Laravel wrapper for Deepseek PHP client to seamless deepseek AI API integration with Laravel applications. |
| 275 | + |
| 276 | +- <a href="https://github.com/qwen-php/qwen-php-client"><img src="https://avatars.githubusercontent.com/u/197095442?s=200&v=4" width="26" height="26" style="border-radius:13px;" alt="Qwen PHP client" /> **Qwen PHP client** </a> robust and community-driven PHP client library for seamless integration with the Qwen API . |
| 277 | + |
| 278 | +- <a href="https://github.com/qwen-php/qwen-laravel"><img src="https://github.com/qwen-php/qwen-laravel/blob/master/public/images/laravel%20qwen%20ai%20banner.jpg?raw=true" width="26" height="26" style="border-radius:13px;" alt="qwen laravel" /> **Laravel qwen** </a> wrapper for qwen PHP client to seamless Alibaba qwen AI API integration with Laravel applications.. |
| 279 | + |
| 280 | +### Dashboards |
| 281 | + |
| 282 | +- <a href="https://github.com/omaralalwi/laravel-startkit"><img src="https://raw.githubusercontent.com/omaralalwi/laravel-startkit/master/public/screenshots/backend-rtl.png" width="26" height="26" style="border-radius:13px;" alt="Laravel Startkit" /> **Laravel Startkit** </a> Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects. |
| 283 | + |
| 284 | +- <a href="https://github.com/kunafaPlus/kunafa-dashboard-vue"><img src="https://github.com/kunafaPlus/kunafa-dashboard-vue/raw/master/public/screenshots/Home-LTR.png" width="26" height="26" style="border-radius:13px;" alt="Kunafa Dashboard Vue" /> **Kunafa Dashboard Vue** </a> A feature-rich Vue.js 3 dashboard template with multi-language support and full RTL/LTR bidirectional layout capabilities. |
| 285 | + |
| 286 | +### References |
| 287 | + |
| 288 | +- <a href="https://github.com/omaralalwi/clean-code-summary"><img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="26" height="26" style="border-radius:13px;" alt="Clean Code Summary" /> **Clean Code Summary** </a> summarize and notes for books and practices about clean code. |
| 289 | + |
| 290 | +- <a href="https://github.com/omaralalwi/solid-principles-summary"><img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="26" height="26" style="border-radius:13px;" alt="SOLID Principles Summary" /> **SOLID Principles Summary** </a> summarize and notes for books about SOLID Principles. |
0 commit comments