beholdr / laravel-helpers
Some helpers for Laravel.
Installs: 1 472
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/beholdr/laravel-helpers
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
- livewire/livewire: ^3.6
- 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:/']); ?>
PermanentRedirects middleware
Replaces all 302 redirects with 301 (for SEO purposes).
Add in bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \Beholdr\LaravelHelpers\Middleware\PermanentRedirects::class, ]);
RemoveIndex middleware
Removes trailing /index from URLs, making a redirect /url/index → /url.
Useful for folio pages.
Add in bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \Beholdr\LaravelHelpers\Middleware\RemoveIndex::class, ]);
IgnoreEmbeddableCsrfToken middleware
Ignores CSRF tokens validation for embedded Livewire components, like when you need to load component in iframe.
First, add Embeddable attribute to a component class:
use Beholdr\LaravelHelpers\Attributes\Embeddable; #[Embeddable] class Calculator extends Component {}
Then add in bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->web(replace: [ \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class => \Beholdr\LaravelHelpers\Middleware\IgnoreEmbeddableCsrfToken::class, ]);
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, ]);
SimpleAuth middleware
Simple basic auth middleware with given credentials for a route or folio page.
Add alias in bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'simple_auth' => \Beholdr\LaravelHelpers\Middleware\SimpleAuth::class, ]);
Then add at the folio page:
<?php use function Laravel\Folio\middleware; middleware('simple_auth:username,passw0rd'); ?>
FromUrl attribute for Livewire
When you need to load initial value of the Livewire property from the URL, but do not need to update URL on property change:
use Beholdr\LaravelHelpers\Attributes\FromUrl; class Calculator extends Component { #[FromUrl] public $country; }
UtmFields enum
Enum UtmFields is used for processing 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.
Turned on by default, can be disabled by HTTP_CLIENT_LOG_ENABLED variable or with http_client_log config option.
Telegram log channel
Custom log channel TelegramLogChannel sending 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.