beholdr / laravel-helpers
Some helpers for Laravel.
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
Some helpers for Laravel projects.
Installation
You can install the package via composer:
composer require beholdr/laravel-helpers
You can publish the config file with:
php artisan vendor:publish --tag="helpers-config"
This is the contents of the published config file:
return [ 'http_client_log' => true, ];
Usage
Redirect middleware
Simple redirect middleware.
Add an alias in bootstrap/app.php
:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ // other middleware aliases... 'redirect' => \Beholdr\LaravelHelpers\Middleware\Redirect::class, ]);
Example of usage in Folio page:
<?php use function Laravel\Folio\middleware; // redirect by route name middleware(['redirect:route.cards.unistream']); // OR redirect by URL middleware(['redirect:/']); ?>
RemoveTrailingSlash middleware
Removes trailing slashes from URLs, making a redirect /some/url/
→ /some/url
.
Add in bootstrap/app.php
:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \Beholdr\LaravelHelpers\Middleware\RemoveTrailingSlash::class, ]);
UtmFields enum
Enum UtmFields
is used for processing of UTM analytics tags.
To get an array of UTM parameters, exluding all other query variables:
use Beholdr\LaravelHelpers\Enums\UtmFields; UtmFields::fromQuery(request()->getQueryString()); // ['utm_content' => '...', 'utm_source' => '...']
HttpClient logger
Automatically logs all HttpClient requests: both success and failure.
Can be disabled via http_client_log
config option.
Telegram log alerts
Custom log channel TelegramLogChannel
sends alert to your telegram bot upon a log event with a defined level.
Add in your config/logging.php
:
'channels' => [ // other channels... 'telegram' => [ 'driver' => 'custom', 'via' => \Beholdr\LaravelHelpers\Logging\TelegramLogChannel::class, 'token' => env('TELEGRAM_BOT_TOKEN'), 'channel' => env('TELEGRAM_CHAT_ID'), 'level' => env('TELEGRAM_LOG_LEVEL', \Monolog\Level::Error), ], ]
And then define in your .env
:
LOG_STACK=daily,telegram
TELEGRAM_BOT_TOKEN=#####
TELEGRAM_CHAT_ID=#####
Where TELEGRAM_BOT_TOKEN
and TELEGRAM_CHAT_ID
contains credentials for your telegram bot and channel ID.
AppException
Universal exception class Beholdr\LaravelHelpers\Exceptions\AppException
to wrap other exceptions and forward to a client.
Can define HTTP statusCode
(500
by default) and disable reporting in logs.