ViewDataProvider
in package
Global view data provider for the MVC framework.
This class manages globally-shared view data that should be automatically injected into all views. It eliminates the need for views to directly access the Registry and provides a centralized place to configure view-level globals.
- Register in an Initializer during application bootstrap
- Share data using share() method with static values or callables
- Data automatically injected into all views via Base::injectHelpers()
Tags
Table of Contents
Properties
- $_data : array<string|int, mixed>
- $_instance : ViewDataProvider|null
Methods
- all() : array<string|int, mixed>
- Get all shared data with callables resolved.
- clear() : ViewDataProvider
- Clear all shared data.
- count() : int
- Get count of shared data items.
- get() : mixed
- Get a shared value by key.
- getInstance() : ViewDataProvider
- Get the singleton instance of ViewDataProvider.
- has() : bool
- Check if a key exists in the shared data.
- remove() : ViewDataProvider
- Remove a shared value.
- share() : ViewDataProvider
- Share a value globally with all views.
- shareMultiple() : ViewDataProvider
- Share multiple values at once.
- __construct() : mixed
- Private constructor to enforce singleton pattern
Properties
$_data
private
array<string|int, mixed>
$_data
= []
$_instance
private
static ViewDataProvider|null
$_instance
= null
Methods
all()
Get all shared data with callables resolved.
public
all() : array<string|int, mixed>
This resolves all callables before returning, so the returned array contains only static values ready for view rendering.
Return values
array<string|int, mixed> —Associative array of all shared data with resolved values
clear()
Clear all shared data.
public
clear() : ViewDataProvider
Return values
ViewDataProvider —Fluent interface
count()
Get count of shared data items.
public
count() : int
Return values
int —Number of shared items
get()
Get a shared value by key.
public
get(string $key[, mixed $default = null ]) : mixed
If the value is a callable, it will be executed and the result returned. If the key doesn't exist, returns the default value.
Parameters
- $key : string
-
The key to retrieve
- $default : mixed = null
-
Default value if key doesn't exist
Return values
mixed —The resolved value
getInstance()
Get the singleton instance of ViewDataProvider.
public
static getInstance() : ViewDataProvider
Creates and registers the instance in Registry if it doesn't exist.
Return values
ViewDataProviderhas()
Check if a key exists in the shared data.
public
has(string $key) : bool
Parameters
- $key : string
-
The key to check
Return values
bool —True if key exists, false otherwise
remove()
Remove a shared value.
public
remove(string $key) : ViewDataProvider
Parameters
- $key : string
-
The key to remove
Return values
ViewDataProvider —Fluent interface
share()
Share a value globally with all views.
public
share(string $key, mixed $value) : ViewDataProvider
The value can be a static value or a callable. If a callable is provided, it will be executed each time the view data is resolved, allowing for dynamic values that change per request.
Parameters
- $key : string
-
The key to store the data under
- $value : mixed
-
The value to share (can be callable for lazy evaluation)
Tags
Return values
ViewDataProvider —Fluent interface
shareMultiple()
Share multiple values at once.
public
shareMultiple(array<string|int, mixed> $data) : ViewDataProvider
Parameters
- $data : array<string|int, mixed>
-
Associative array of key-value pairs
Tags
Return values
ViewDataProvider —Fluent interface
__construct()
Private constructor to enforce singleton pattern
private
__construct() : mixed