laravel-helpers maintained by beholdr
Laravel helpers
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' => env('HTTP_CLIENT_LOG_ENABLED', true),
'http_client_log_limit' => env('HTTP_CLIENT_LOG_LIMIT', 1024),
];
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:/']);
?>
RedirectUnlessConfig middleware
If the configuration does not contain a given key, redirect to the specified URL or route. Useful for disabling a page without a corresponding feature flag.
Add an alias in bootstrap/app.php:
return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
// other middleware aliases...
'redirectUnlessConfig' => \Beholdr\LaravelHelpers\Middleware\RedirectUnlessConfig::class,
]);
Example of usage in Folio page:
<?php
use function Laravel\Folio\middleware;
// redirect by route name if `custom.features.feature_xxx_enabled` not set
middleware(['redirectUnlessConfig:custom.features.feature_xxx_enabled,route.cards.unistream']);
// OR redirect by URL if `custom.features.feature_xxx_enabled` not set
middleware(['redirectUnlessConfig:custom.features.feature_xxx_enabled,/']);
?>
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.
Also removes duplicate slashes inside URLs like: ///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([
'simpleAuth' => \Beholdr\LaravelHelpers\Middleware\SimpleAuth::class,
]);
Then add at the folio page:
<?php
use function Laravel\Folio\middleware;
middleware('simpleAuth: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.
Also you can set limit for truncation of the request/response body with HTTP_CLIENT_LOG_LIMIT variable or with http_client_log_limit 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),
'deduplication_time' => 300, // 5 minutes
],
]
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.