Neuron-PHP

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.

  1. Register in an Initializer during application bootstrap
  2. Share data using share() method with static values or callables
  3. Data automatically injected into all views via Base::injectHelpers()
Tags
example
// In an Initializer:
$provider = ViewDataProvider::getInstance();

// Static values
$provider->share('siteName', 'My Website');

// Lazy evaluation (callable executed on each request)
$provider->share('currentUser', function() {
    return Registry::getInstance()->get('Auth.User');
});

// Multiple values at once
$provider->shareMultiple([
    'theme' => 'default',
    'year' => fn() => date('Y')
]);

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

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

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

has()

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

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
example
$provider->share('siteName', 'My Website');
$provider->share('currentUser', fn() => Auth::user());
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
example
$provider->shareMultiple([
    'siteName' => 'My Website',
    'theme' => 'dark',
    'currentYear' => fn() => date('Y')
]);
Return values
ViewDataProvider

Fluent interface

__construct()

Private constructor to enforce singleton pattern

private __construct() : mixed

        
On this page

Search results