ViewContext
in package
Fluent view context builder for controllers.
This class provides a clean, fluent API for building view data and rendering views.
Tags
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
$_controller
private
Base
$_controller
$_data
private
array<string|int, mixed>
$_data
= []
$_description
private
string|null
$_description
= null
$_registry
private
Registry
$_registry
$_status
private
HttpResponseStatus
$_status
= \Neuron\Mvc\Responses\HttpResponseStatus::OK
$_title
private
string|null
$_title
= null
Methods
__construct()
public
__construct(Base $controller[, Registry|null $registry = null ]) : mixed
Parameters
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
BasegetData()
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|nullgetStatus()
Get the HTTP response status.
public
getStatus() : HttpResponseStatus
Return values
HttpResponseStatusgetTitle()
Get the current title.
public
getTitle() : string|null
Return values
string|nullrender()
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
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
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
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
status()
Set the HTTP response status.
public
status(HttpResponseStatus $status) : ViewContext
Parameters
- $status : HttpResponseStatus
-
The HTTP status code
Return values
ViewContext —Fluent interface
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:
- Key-value: ->with('posts', $posts)
- 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:
- Adds title and description (if set and $includeMetadata is true)
- Auto-injects user if requested
- Auto-injects CSRF token if requested
- 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