beholdr/laravel-helpers

Some helpers for Laravel.

v0.0.1 2025-04-16 10:27 UTC

This package is auto-updated.

Last update: 2025-04-16 10:27:31 UTC


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.