beholdr/laravel-helpers
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' => '...']
AutoRoles trait
Auto assign roles for your model on creation. Works with spatie/laravel-permission:
use Beholdr\LaravelHelpers\Attributes\AutoRoles;
use Beholdr\LaravelHelpers\Traits\HasAutoRoles;
use Spatie\Permission\Traits\HasRoles;
#[AutoRoles(['member', 'editor'])]
class User extends Authenticatable
{
use HasRoles;
use HasAutoRoles;
}
It also supports enums:
use App\Enums\UserRole;
#[AutoRoles(UserRole::MEMBER)]
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.
Ntfy log channel
Custom log channel NtfyLogChannel sending alert to your ntfy.sh channel upon a log event with a defined level.
Add in your config/logging.php:
'channels' => [
// other channels...
'ntfy' => [
'driver' => 'custom',
'via' => \Beholdr\LaravelHelpers\Logging\NtfyLogChannel::class,
'server' => env('NTFY_SERVER', 'https://ntfy.sh'),
'topic' => env('NTFY_TOPIC'),
'level' => env('NTFY_LOG_LEVEL', \Monolog\Level::Error),
'deduplication_time' => 300, // 5 minutes
],
]
And then define in your .env:
LOG_STACK=daily,ntfy
NTFY_TOPIC=#####
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.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v0.16.0 | ^8.3 | ^11.0| | MIT | |
| v0.15.0 | ^8.3 | ^11.0| | MIT | |
| v0.14.1 | ^8.3 | ^11.0| | MIT | |
| v0.14.0 | ^8.3 | ^11.0| | MIT | |
| v0.13 | ^8.3 | ^11.0| | MIT | |
| v0.12.1 | ^8.3 | ^11.0| | MIT | |
| v0.12.0 | ^8.3 | ^11.0| | MIT | |
| v0.11.0 | ^8.3 | ^11.0| | MIT | |
| v0.10.0 | ^8.3 | ^11.0| | MIT | |
| v0.9.2 | ^8.3 | ^11.0| | MIT | |
| v0.9.1 | ^8.3 | ^11.0| | MIT | |
| v0.9.0 | ^8.3 | ^11.0| | MIT | |
| v0.8.0 | ^8.3 | ^11.0| | MIT | |
| v0.7.0 | ^8.3 | ^11.0| | MIT | |
| v0.6.0 | ^8.3 | ^11.0| | MIT | |
| v0.5.2 | ^8.3 | ^11.0| | MIT | |
| v0.5.1 | ^8.3 | ^11.0| | MIT | |
| v0.5.0 | ^8.3 | ^11.0| | MIT | |
| v0.4.0 | ^8.3 | ^11.0| | MIT | |
| v0.3.0 | ^8.3 | ^11.0| | MIT | |
| v0.2.0 | ^8.3 | ^11.0| | MIT | |
| v0.0.1 | ^8.3 | ^11.0| | MIT |