# Laravel integration (laravel-rust-barcode)

> Add the adapter with cargo add laravel-rust-barcode@0.1.

- Package: laravel-rust-barcode (Laravel for Rust, alpha), crates: `laravel-rust-barcode`, `laravel-rust-barcode-core`
- Maintenance: active; Latest release: 0.1.0 (2026-12-05); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-barcode), [crates.io](https://crates.io/crates/laravel-rust-barcode/0.1.0), [docs.rs](https://docs.rs/laravel-rust-barcode/0.1.0), [Changelog](https://oss.bowerbird.si/barcode/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-barcode/blob/3cc853b5abd7af2bf6028407dda457fb69bdf231/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-barcode/blob/3cc853b5abd7af2bf6028407dda457fb69bdf231/SECURITY.md)
- Based on: [milon/barcode](https://github.com/milon/barcode): The PHP package whose behaviour this package independently implements, following version 13.6.0.
- PHP parity: full (66 covered, 13 diverged, 0 planned of 79 abilities; 205 fixtures; follows milon/barcode v13.6.0; verified 2026-12-05)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/barcode/docs/laravel/
- Source: https://github.com/bowerbird-si/laravel-rust-barcode/blob/3cc853b5abd7af2bf6028407dda457fb69bdf231/docs/laravel.md

Add the adapter with `cargo add laravel-rust-barcode@0.1`.

Register the barcode provider to use linear and matrix encoders from the Laravel container or MiniJinja templates.

```rust
use laravel::Application;
use laravel_rust_barcode::BarcodeServiceProvider;

let app = Application::configure()
    .with_providers(|providers| { providers.add(BarcodeServiceProvider); })
    .create();
```

The provider binds `Dns1d` and `Dns2d` as immutable singletons. Render options belong to each call.

| Variable | Default | Effect |
| --- | --- | --- |
| `BARCODE_STORE_PATH` | Application public directory | Existing directory for image files |
| `BARCODE_PUBLIC_PATH` | Site root | Public path prefix for saved files |

Public URLs combine `APP_URL`, the configured prefix, and the encoded filename. Set
`BarcodeConfig` in the application's configuration callback to override these settings in code.

## Linear and matrix services

```rust
use laravel_rust_barcode::{Dns1d, Dns2d, core::{GridStyle, RasterOptions}};

let dns1d = app.resolve::<Dns1d>().await;
let png = dns1d.png("HELLO", "C39+", true, RasterOptions::default())?;
let file = dns1d.store(&png, "HELLO", None)?;
let url = dns1d.uri(&file);

let dns2d = app.resolve::<Dns2d>().await;
let svg = dns2d.svg("https://example.com", "QRCODE,M", GridStyle::default())?;
```

Both services accept PHP type names without regard to ASCII case. Use `Dns1d` for SVG, HTML,
PNG or JPEG. Use `Dns2d` for SVG, HTML or PNG. A 2D type string may include QR or PDF417 options.
The matrix PNG method accepts an optional `Logo` decoded from image bytes.

The default filename uses a lowercase slug. For 2D symbols it also includes the full type string.
The slug transliterates Latin-1 and Latin Extended-A letters and drops other scripts. A custom file
name keeps the last path component and takes its extension from the renderer. The file is replaced
atomically.

## Templates

The default `minijinja` feature registers these template functions.

| Function | Output |
| --- | --- |
| `barcode_svg(code, kind)` | Inline linear SVG with a label |
| `barcode_html(code, kind)` | Linear HTML without a label |
| `barcode_2d_svg(code, kind)` | Matrix SVG |

```jinja
{{ barcode_svg(order.reference, "C128") }}
{{ barcode_2d_svg(order.url, "QRCODE,M") }}
```

The renderer escapes the label text before returning safe markup. Invalid inputs return a template
error. Use the services for custom dimensions and colours. Disable default features to omit the
template functions.


## Binary payloads and static serving

For raw Code 128 function bytes, call `Dns1d::encode` with a byte slice and render the returned
symbol directly. The rendering shortcuts and templates take UTF-8 strings. For PDF417 options
containing the legacy raw `0xFF` comma escape, call `core::Pdf417Options::from_type_bytes` with a byte slice. The escape is allowed in optional text fields, not in numeric file IDs.

On Unix, the atomic store creates files readable only by the application user. Serve them as
that user, or manage file permissions separately if a static server runs under another account.

Use a dedicated barcode store for request-driven writes. Slugs can collide and each write replaces
the existing image, including its permissions. Avoid accepting caller-chosen names in shared stores.
Use `uri()` for an encoded URL. The core's explicit `Store::temporary()` is intended for local use,
not shared production storage. The Laravel provider instead defaults to the application's public
directory when no path is configured.

Rendering is synchronous. In services handling concurrent requests, set smaller application-level
dimension limits and bound the number of blocking render tasks.
