Download the PHP package edulazaro/laratox without Composer
On this page you can find all versions of the php package edulazaro/laratox. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edulazaro/laratox
More information about edulazaro/laratox
Files in edulazaro/laratox
Package laratox
Short Description Content moderation for Laravel, powered by ToxicFilter
License MIT
Homepage https://toxicfilter.com/docs/sdks
Informations about the package laratox
Laratox
Content moderation for Laravel, powered by ToxicFilter.
Every check returns one of three decisions (allow, review, block), the categories that fired (spam, scams, harassment, personal data… fifteen in all) and the reason in words.
Installation
Requires PHP 8.2+ and Laravel 12 or 13.
Create a key in your ToxicFilter account (the free plan
is enough) and add it to .env:
A tf_test_ key does the same work and is never charged. Check it with:
Checking content
Say what it is, then check():
Options chain before check():
Also ->actor(), ->withoutAi(), ->rules([...]), ->redact() and ->option($key, $value) for anything else.
If your ToxicFilter organization moderates several sites, give each app its project once in
.env (TOXICFILTER_PROJECT=forum): every check and every ToxicFilter::batch() is filed
there, with its own activity, review queue and webhooks. ->project() overrides it for one call.
Every kind of content:
The verdict is the PHP SDK's: allowed(), needsReview(), blocked(), reason() (the first one), reasons() (all of them), scores(), flagged(), id(), project()… The rest of the API is there too: ToxicFilter::batch(), records(), resolve(), feedback(), usage(), ping(), and ToxicFilter::client() gives you the SDK client itself.
When the API cannot answer
check() throws the SDK's exceptions. Catch the ones you want to handle:
| Exception | When |
|---|---|
AuthenticationError |
The key is wrong |
QuotaExhausted |
Not enough credits for this call |
InvalidRequest |
A field was rejected: $e->fields() |
RateLimited |
Too many calls, after the retries |
ServerError |
The service is down or unreachable, after the retries |
All of them extend ToxicFilter\Exception\ApiError. The validation rule below catches them for you.
In a form
The rule chains the same options as ToxicFilter::text(). A block fails the field; a
review passes, and ->orReview() fails it too. Put the rule last, after bail, since it
is a network call.
The message carries ToxicFilter's reason: "The body could not be accepted: contains a phone
number." Reasons are written in English, so by default (rule.show_reason = auto) they are
only shown when your app's locale is English; other languages get the message without one.
To act on the decision afterwards, keep the rule:
With a wildcard ('tags.*' => [$rule]), ask for each field: $rule->verdict('tags.0').
If the API cannot answer, laratox.rule.on_error decides: allow (default) lets the field
through and logs a warning, refuse fails it.
Testing
The fake replaces only the network: the SDK's client and verdicts are the real ones. Like the API, it refuses more than ten locales with an InvalidRequest.
Configuration
| Key | Env | Default |
|---|---|---|
key |
TOXICFILTER_KEY |
|
project |
TOXICFILTER_PROJECT |
your organization's default project |
url |
TOXICFILTER_URL |
https://toxicfilter.com |
timeout |
TOXICFILTER_TIMEOUT |
10 |
connect_timeout |
TOXICFILTER_CONNECT_TIMEOUT |
5 |
retries |
TOXICFILTER_RETRIES |
2 |
rule.on_error |
TOXICFILTER_ON_ERROR |
allow |
rule.show_reason |
auto (true, false, or only when the locale is English) |
Calls go through Laravel's HTTP client, so Http::fake() and your logging see them. A 429 or 5xx is retried with the same idempotency key, so it is judged and billed once; a 402 is never retried.
Sponsors
Laratox is supported by the following sponsors. Thank you for keeping it growing:
Author
Created by Edu Lazaro
License
Laratox is open-sourced software licensed under the MIT license.
All versions of laratox with dependencies
laravel/framework Version ^12.61.1|^13.12
edulazaro/toxicfilter-sdk Version ^1.6