Skip to content

Helpers

David Ciulla edited this page Nov 7, 2026 · 2 revisions

Helpers

Laravex.Support.Helpers is a collection of Laravel-style helper functions. Import the module where needed, or call functions via their full module path.

import Laravex.Support.Helpers

Paths

base_path()           # → "/var/www/app"
base_path("routes")   # → "/var/www/app/routes"

storage_path()        # → "/var/www/app/storage"
storage_path("logs")  # → "/var/www/app/storage/logs"

public_path()         # → "/var/www/app/public"
public_path("js")     # → "/var/www/app/public/js"

resource_path()       # → "/var/www/app/resources"
resource_path("views")

database_path()       # → "/var/www/app/database"
database_path("migrations")

URLs

# Generate a URL for a public asset (uses APP_URL from config)
asset("js/app.js")
# => "https://example.com/js/app.js"

# Generate a URL for a file on a storage disk (defaults to "public" disk)
storage_url("images/avatar.png")
storage_url("reports/data.csv", "local")

# Generate a named route URL
route("users.show", %{id: 1})
# => "/users/1"

# Generate a signed route URL (permanent)
signed_route("unsubscribe", %{email: "alice@example.com"})

# Generate a temporary signed URL (expires after N seconds)
temporary_signed_route("download", 3600, %{file: "report.pdf"})

Responses and Redirects

# Return an HTTP error (raises Laravex.Http.HttpException)
abort(404)
abort(403, "Forbidden")

# Redirect to a URL
redirect("/dashboard")

# Redirect to a named route
to_route("dashboard")

# Redirect to an external URL
away("https://example.com")

# Redirect back to the previous page (uses Referer header or session fallback)
back()
back("/dashboard")  # fallback when Referer is absent

# Redirect with flash data
redirect_with("/dashboard", %{"status" => "Profile updated."})

# Redirect and re-populate the form with old input
redirect_with_input("/profile/edit")

# Store the intended URL for post-login redirect
intended("/checkout")

# Redirect to the previously intended URL
redirect_intended()
redirect_intended("/home")  # fallback

Views

# Render a view template
view("dashboard", %{user: user, posts: posts})

This delegates to Laravex.View.make/2. See Views for details.


Configuration and Environment

# Read a config value with dot notation
config("app.name")
config("mail.from.address", "hello@example.com")

# Read an environment variable
env("APP_KEY")
env("PORT", "8000")

Sessions and Flash

# Read a value from the current request's session
session_get("user_id")
session_get("cart", %{})

# Flash a value to the session (available on the next request)
flash("status", "Profile updated.")

# Flash a value visible only on the current request (not persisted)
flash_now("error", "Something went wrong.")

# Retrieve old input (populated after redirect_with_input)
old("email")
old("name", "")

Forms

# Retrieve the validated input map (or a single key) from the current request
validated()           # => %{"email" => "alice@example.com", …}
validated("email")    # => "alice@example.com"

Cookies

# Read a cookie from the current request
cookie("remember_token")

# Queue a cookie to be set on the response
cookie("remember_token", token, max_age: 2_592_000)

Collections

# Create a Collection from any enumerable
collect([1, 2, 3])
collect(App.Models.Post.all())

See Collections for the full Collection API.


Strings

# Generate a cryptographically random URL-safe string
str_random()     # 16 characters (default)
str_random(32)   # 32 characters

# Access the Str helper module
str().slug("Hello World")   # => "hello-world"
str().uuid()                # => "550e8400-e29b-41d4-a716-446655440000"

See Strings for the full Laravex.Support.Str API.


Blank / Filled Checks

# Returns true for nil, false, "", [], %{}, or whitespace-only strings
blank?(nil)         # => true
blank?("")          # => true
blank?([])          # => true
blank?(%{})         # => true
blank?("  ")        # => true
blank?("hello")     # => false

filled?(value)      # => inverse of blank?

Throw Helpers

# Raise when condition is truthy (Laravel `throw_if`)
throw_if(invalid?, "Validation failed")           # raises RuntimeError
throw_if(invalid?, MyError)                       # raises MyError
throw_if(invalid?, MyError, "Custom message")

# Raise when condition is falsy (Laravel `throw_unless`)
throw_unless(authorized?, "Forbidden")
throw_unless(authorized?, Laravex.Http.HttpException, "Forbidden")

Previous URL

# Returns the URL the user came from (session-stored previous_url)
previous_url()
previous_url("/fallback")  # optional fallback when no previous URL is stored

Debug

# Dump a value and halt (raises DumpException)
dd(user)

# Dump a value and continue
dump(user)

Facade Shortcuts

These functions return the relevant module so you can call its functions without a full module path:

auth()     # => Laravex.Auth.Guard
cache()    # => Laravex.Cache.Manager
gate()     # => Laravex.Auth.Gate
session()  # => Laravex.Session.Store
storage()  # => Laravex.Filesystem.Storage
http()     # => Laravex.Http.Client
bus()      # => Laravex.Bus
str()      # => Laravex.Support.Str
arr()      # => Laravex.Support.Arr
cache().put("users", users, 600)
http().get("https://api.example.com/users")
gate().allows?(:update_post, post)

Localization

# Translate a string (shorthand for Laravex.Translation.get/3)
___("auth.failed")
___("validation.email", %{attribute: "email"})
___("messages.greeting", %{name: "Alice"}, "fr")

See Localization for the full translation system.


Pagination

paginator_links(pager, "/posts")

Returns an HTML pagination link string. See the Routing and Views pages for context.


Now

now()
# => ~U[2026-11-07 07:59:00Z]  (DateTime.utc_now())

See Also

Clone this wiki locally