Neuron-PHP

ViewContext
in package

Fluent view context builder for controllers.

This class provides a clean, fluent API for building view data and rendering views.

Tags
example
// Basic usage
return $this->view()
    ->title('Dashboard')
    ->description('Admin Dashboard')
    ->with('posts', $posts)
    ->render('index', 'admin');

// With multiple data items
return $this->view()
    ->title('Edit Post')
    ->with([
        'post' => $post,
        'categories' => $categories,
        'tags' => $tags
    ])
    ->render('edit');

// Auto-inject user and CSRF token
return $this->view()
    ->title('Profile')
    ->withCurrentUser()
    ->withCsrfToken()
    ->render('profile');

// Custom response status
return $this->view()
    ->status(HttpResponseStatus::CREATED)
    ->title('Post Created')
    ->with('post', $post)
    ->render('show');

// Cache control
return $this->view()
    ->title('Homepage')
    ->cache(true)
    ->with('featured', $posts)
    ->render('index');

Table of Contents

Properties

$_autoInjectCsrf  : bool
$_autoInjectUser  : bool
$_cacheEnabled  : bool|null
$_controller  : Base
$_data  : array<string|int, mixed>
$_description  : string|null
$_registry  : Registry
$_status  : HttpResponseStatus
$_title  : string|null

Methods

__construct()  : mixed
cache()  : ViewContext
Enable or disable view caching.
description()  : ViewContext
Set the page description (meta description).
getController()  : Base
Get the controller instance.
getData()  : array<string|int, mixed>
Get the current view data (without metadata).
getDescription()  : string|null
Get the current description.
getStatus()  : HttpResponseStatus
Get the HTTP response status.
getTitle()  : string|null
Get the current title.
render()  : string
Alias for renderHtml() - the default render method.
renderHtml()  : string
Render the view as HTML.
renderJson()  : string
Render the view as JSON.
renderMarkdown()  : string
Render the view as Markdown.
renderXml()  : string
Render the view as XML.
status()  : ViewContext
Set the HTTP response status.
title()  : ViewContext
Set the page title.
with()  : ViewContext
Add custom data to the view.
withCsrfToken()  : ViewContext
Auto-inject a CSRF token into view data and Registry.
withCurrentUser()  : ViewContext
Auto-inject the current authenticated user into view data.
buildViewData()  : array<string|int, mixed>
Build the final view data array.

Properties

$_autoInjectCsrf

private bool $_autoInjectCsrf = false

$_autoInjectUser

private bool $_autoInjectUser = false

$_cacheEnabled

private bool|null $_cacheEnabled = null

$_description

private string|null $_description = null

Methods

__construct()

public __construct(Base $controller[, Registry|null $registry = null ]) : mixed
Parameters
$controller : Base

The controller instance

$registry : Registry|null = null

Optional Registry instance for dependency injection (defaults to singleton)

cache()

Enable or disable view caching.

public cache(bool $enabled) : ViewContext
Parameters
$enabled : bool

Whether caching is enabled

Return values
ViewContext

Fluent interface

description()

Set the page description (meta description).

public description(string $description) : ViewContext
Parameters
$description : string

The page description

Return values
ViewContext

Fluent interface

getController()

Get the controller instance.

public getController() : Base
Return values
Base

getData()

Get the current view data (without metadata).

public getData() : array<string|int, mixed>

Useful for debugging or testing.

Return values
array<string|int, mixed>

getDescription()

Get the current description.

public getDescription() : string|null
Return values
string|null

getTitle()

Get the current title.

public getTitle() : string|null
Return values
string|null

render()

Alias for renderHtml() - the default render method.

public render([string $page = 'index' ][, string $layout = 'default' ]) : string
Parameters
$page : string = 'index'

The page template name

$layout : string = 'default'

The layout template name

Tags
throws
NotFound

If template not found

Return values
string

The rendered HTML

renderHtml()

Render the view as HTML.

public renderHtml([string $page = 'index' ][, string $layout = 'default' ]) : string
Parameters
$page : string = 'index'

The page template name

$layout : string = 'default'

The layout template name

Tags
throws
NotFound

If template not found

Return values
string

The rendered HTML

renderJson()

Render the view as JSON.

public renderJson() : string

Note: title and description are ignored for JSON rendering.

Return values
string

The rendered JSON

renderMarkdown()

Render the view as Markdown.

public renderMarkdown([string $page = 'index' ][, string $layout = 'default' ]) : string
Parameters
$page : string = 'index'

The page template name

$layout : string = 'default'

The layout template name

Tags
throws
NotFound

If template not found

throws
CommonMarkException

If markdown parsing fails

Return values
string

The rendered Markdown/HTML

renderXml()

Render the view as XML.

public renderXml() : string

Note: title and description are ignored for XML rendering.

Return values
string

The rendered XML

title()

Set the page title.

public title(string $title) : ViewContext

The title will be automatically concatenated with the site name when rendering (e.g., "Dashboard | My Site").

Parameters
$title : string

The page title

Return values
ViewContext

Fluent interface

with()

Add custom data to the view.

public with(string|array<string|int, mixed> $key[, mixed $value = null ]) : ViewContext

Supports two usage patterns:

  1. Key-value: ->with('posts', $posts)
  2. Array merge: ->with(['posts' => $posts, 'count' => 10])
Parameters
$key : string|array<string|int, mixed>

Key name or associative array of data

$value : mixed = null

Value to set (ignored if $key is array)

Return values
ViewContext

Fluent interface

withCsrfToken()

Auto-inject a CSRF token into view data and Registry.

public withCsrfToken() : ViewContext

Looks up 'Auth.CsrfToken' from Registry and makes it available as $CsrfToken in the view template.

For CMS applications, the token should be set via an initializer or middleware before controller execution.

Return values
ViewContext

Fluent interface

withCurrentUser()

Auto-inject the current authenticated user into view data.

public withCurrentUser() : ViewContext

The user will be available as $User in the view template. Looks up 'Auth.User' from Registry.

Return values
ViewContext

Fluent interface

buildViewData()

Build the final view data array.

private buildViewData([bool $includeMetadata = true ]) : array<string|int, mixed>

This method:

  1. Adds title and description (if set and $includeMetadata is true)
  2. Auto-injects user if requested
  3. Auto-injects CSRF token if requested
  4. Merges with custom data provided via with()

Note: The resulting array will be further processed by Base::injectHelpers() which merges ViewDataProvider global data and adds UrlHelper.

Parameters
$includeMetadata : bool = true

Whether to include title/description

Return values
array<string|int, mixed>

The view data array


        
On this page

Search results