UrlHelper
in package
Rails-style URL helper for generating URLs from named routes.
This helper provides convenient methods for generating URLs from route names with parameter substitution, supporting both relative and absolute URLs. It integrates with the Neuron routing system to provide Rails-like helpers for view templates and controllers.
Key features:
- Generate URLs from named routes with parameter substitution
- Support for both relative and absolute URL generation
- Rails-style helper method naming (route_path, route_url)
- Integration with Router singleton for route lookup
- Error handling for missing routes and parameters
- Fluent interface for method chaining
URL helpers follow Rails conventions:
*_path()methods generate relative URLs*_url()methods generate absolute URLs- Parameters are passed as associative arrays
- Missing routes return null for graceful error handling
Tags
Table of Contents
Properties
Methods
- __call() : string|null
- Magic method to provide Rails-style helper methods.
- __construct() : mixed
- UrlHelper constructor.
- getAvailableRoutes() : array<string|int, mixed>
- Get all available named routes for debugging.
- getRouter() : Router
- Get the Router instance being used.
- routeExists() : bool
- Check if a named route exists.
- routePath() : string|null
- Generate a relative URL for a named route.
- routeUrl() : string|null
- Generate an absolute URL for a named route.
- setRouter() : UrlHelper
- Set a different Router instance.
Properties
$router
private
Router
$router
Methods
__call()
Magic method to provide Rails-style helper methods.
public
__call(string $method, array<string|int, mixed> $arguments) : string|null
Converts camelCase method names to snake_case route names and generates URLs. Supports both *Path() and *Url() method patterns.
Parameters
- $method : string
-
The method name (e.g., 'userProfilePath', 'userProfileUrl')
- $arguments : array<string|int, mixed>
-
Method arguments, first should be parameters array
Tags
Return values
string|null —Generated URL or null if route not found
__construct()
UrlHelper constructor.
public
__construct([Router|null $router = null ]) : mixed
Parameters
- $router : Router|null = null
-
Optional router instance (uses singleton if not provided)
getAvailableRoutes()
Get all available named routes for debugging.
public
getAvailableRoutes() : array<string|int, mixed>
Return values
array<string|int, mixed> —Array of named route information
getRouter()
Get the Router instance being used.
public
getRouter() : Router
Return values
Router —The router instance
routeExists()
Check if a named route exists.
public
routeExists(string $routeName) : bool
Parameters
- $routeName : string
-
The route name to check
Return values
bool —True if route exists, false otherwise
routePath()
Generate a relative URL for a named route.
public
routePath(string $routeName[, array<string|int, mixed> $parameters = [] ]) : string|null
Parameters
- $routeName : string
-
The name of the route
- $parameters : array<string|int, mixed> = []
-
Parameters to substitute in the route path
Return values
string|null —The generated relative URL or null if route not found
routeUrl()
Generate an absolute URL for a named route.
public
routeUrl(string $routeName[, array<string|int, mixed> $parameters = [] ]) : string|null
Parameters
- $routeName : string
-
The name of the route
- $parameters : array<string|int, mixed> = []
-
Parameters to substitute in the route path
Return values
string|null —The generated absolute URL or null if route not found
setRouter()
Set a different Router instance.
public
setRouter(Router $router) : UrlHelper
Parameters
- $router : Router
-
The router to use
Return values
UrlHelper —Fluent interface