PHP Luminova: Template Rendering and View Object
The View class renders templates with optional caching and layout inheritance, ideal for MVC/HMVC apps where you need structured and fast HTML output.
The Luminova's template View class is used to load and render templates content, capable of handling dynamic contents in MVC or HMVC application. It compiles your templates, presents them as HTML or other formats, and can cache the final output to speed up page loads.
Unlike the Content Response Class, which is for sending raw or API data, View focuses on rendering PHP, Smarty or Twig template contents (e.g, html, xml, js, etc.) and allows you to isolate template logics from main application. You can configure the template behavior to match your coding style.
Features
- Fast Rendering – Compiles and minifies templates automatically.
- Flexible Output – Supports templates that return HTML, arrays, objects, or scalars.
- Built-in Caching – Saves rendered templates for better performance.
- Highly Configurable – Adjust rendering modes via Template Rendering Configuration.
Template Rendering Modes
Luminova allows you render templates in two ways: Global Mode (the default) or Isolation Mode.You can switch modes in the Template View Configuration.
Global Mode
Global mode gives your template direct access to $this and self.That means the template can reach:
- view-specific properties
- public methods on the view class
- application properties through
$this->app
// /resources/Views/index.php
// View options
$this->_title;
// Application properties
$this->app->propertyName;Use this when:
You want simple access to everything with no extra setup.Good for quick builds or templates that talk to the application often.
Isolation Mode
In isolation mode, the template cannot use $this or self.Instead, Luminova gives you a clean $self object with only what the view exposes.
// /resources/Views/index.php
// View options
$self->_title;
// Application properties
$self->app->propertyName;Use this when:
You want a stricter boundary between the template and the rest of the app.Useful for cleaner structure, better security, and easier testing.
Smarty or Twig Templates
When you use Smarty or Twig, you can access the view object and the application instance through a global reference. Smarty uses $self, while Twig uses self.
{* /resources/Views/index.tpl *}
{* Smarty View Options *}
{{ $self.title }}
{* Smarty Application Properties *}
{{ $self.app.propertyName }}
{* Twig View Options *}
{{ self.title }}
{* Twig Application Properties *}
{{ self.app.propertyName }}Note:Auto-prefixing of view options does not apply when using Smarty or Twig.If you wish to prefix options, you can do that in controller before rendering template
Template Scope Options
Luminova makes a set of variables automatically available inside every template.These variables give you details about the current view and help you control how the template behaves.
These options are split into two groups: Immutable (system-controlled) and Mutable (you can override them).
Immutable Options
These are set by the system and cannot be changed when rendering a view.
Note:$scope in the below examples refers to:
$thisin Global Mode – Refers to the current view class instance. You can access view methods and application properties directly.$selfin Isolation Mode – Refers to an instance ofLuminova\Template\Engines\Scope, which safely exposes only allowed view properties and the application object.
Note:Accessing
$selfin non-isolation mode will throw aRuntimeException.It is also recommended not to override$selfinside the template file, as it may break the intended scope and guard functionality.
View Extension Type
Variable Name: viewType (string)
Represents the type of template content being rendered (e.g, html, xml etc..).
Examples:
// resources/Views/example.php
// When variable prefixing is enabled
echo $scope->_viewType;
// When variable prefixing is disable
echo $scope->viewType;Current Template
Variable Name: active (string)
The name of the current template file without the extension (e.g, index, about etc..).
Examples:
// resources/Views/example.php
// When variable prefixing is enabled
echo $scope->_active;
// When variable prefixing is disable
echo $scope->active;Assets
Variable Name: asset (string)
The base path to your public assets directory.
Use it as the starting point before adding the final file path.
Examples:
// resources/Views/example.php
// When variable prefixing is enabled
<img src="<?= $scope->_asset; ?>images/file.png">
// When variable prefixing is disable
<img src="<?= $scope->asset; ?>images/file.png">
// Result: /assets/images/file.png{* Smarty *}
<img src="{{ $self.asset }}images/file.png">
{* Twig *}
<img src="{{ self.viewType }}images/file.png">Also a global helper function available e.g,
Luminova\Funcs\asset(...)or{{ asset(...) }}in frontend template engin.
Links
Variable Name: href (string)
The base href used for building links.
Examples:
// resources/Views/example.php
// When variable prefixing is enabled
<a href="<?= $scope->_href; ?>about">About Us</a>
// When variable prefixing is disable
<a href="<?= $scope->href; ?>about">About Us</a>
// Result: /about{* Smarty *}
<a href="{{ $self.href }}images/file.png">About Us</a>
{* Twig *}
<a href="{{ self.href }}images/file.png">About Us</a>Also a global helper function available e.g,
Luminova\Funcs\href(...)or{{ href(...) }}in frontend template engin.
Access Control
Constant Name: ALLOW_ACCESS (bool: true)
A constant you can use to block direct access to view files.
Example:
<?php
defined('ALLOW_ACCESS') || die('Access to file is not allowed');
// Template contents hereThis ensure that only view class can render the template.
Mutable Options
These values can be overridden when rendering a template.
Template Cache
Variable Name: noCache (bool)
If true, the template will be skipped by the caching system if caching is enabled (see env('page.caching')).
Examples:
// /app/Controllers/Http/MyController.php
public function posts(): int
{
return $this->view('posts', [
'noCache' => true
]);
}Page Title
Variable Name: title (string)
The current page title.If not set, Luminova uses: "<active template>" – "<application name>"
Examples:
// /app/Controllers/Http/MyController.php
public function posts(): int
{
return $this->view('posts', [
'title' => 'My Blog Post'
]);
}Page Subtitle
Variable Name: subtitle (string)
The current page subtitle.If not set, it falls back to the title.
Examples:
// /app/Controllers/Http/MyController.php
public function posts(): int
{
return $this->view('posts', [
'subtitle' => 'My Blog Post Contents'
]);
}View Scope Options Access
Template options are extra values passed into a view during rendering.How you access them depends on your Template Variable Prefixing setting in the configuration.
Prefixing Enabled
When prefixing is set to TRUE, Luminova adds an underscore (_) before every option key:
$this->_fooOption;
$self->_fooOption;This applies in both global and isolation modes.
Prefixing Disabled
When prefixing is set to FALSE, options appear as normal property names:
$this->fooOption;
$self->fooOption;Be careful in isolation mode:Do not use
selfas an option name, sinceselfis reserved for the isolated scope object.
Raw Options
With prefixing set to NULL, Luminova skips property injection and exposes the raw $options array directly:
$options['fooOption'];This works the same way in both rendering modes.
You can also fetch values manually using the view helper methods:
getOption()andgetOptions().
Accessing Application Properties
Inside any template, you always have access to your main application instance through $app.
Both protected and public properties or methods in your App\Application class become available.
// /app/Application.php
namespace App;
use Luminova\Foundation\Core\Application as CoreApplication;
class Application extends CoreApplication
{
protected ?Session $session = null;
protected function onCreate(): void
{
$this->session = new Session();
$this->session->setStorage('users');
$this->session->start();
}
}Then inside your view:
// /resources/Views/example.php
$this->app->session->isOnline();
// or in isolation mode
$self->app->session->isOnline();For exporting properties in a controlled way, see the export() method in this template view documentation.
Other Variables
- Variable Name:
$_VIEW_FILEPATH(string) The absolute file path to the current template. - Variable Name:
$_VIEW_TYPE(string) The template view content type.
Examples:
// resources/Views/example.php
echo $_VIEW_FILEPATH;
// /path/application/resources/Views/example.php
echo $_VIEW_TYPE;
// htmlTemplate Contents
Luminova templates support two main ways to provide content: direct output or returning content. How the content is processed depends on its type.
Outputting Content
This is the traditional way, the template directly outputs HTML or text. Use this when you want the template to immediately render to the browser or output buffer.
// resources/Views/example.php
<div>
<p><?= $this->_user->name; ?></p>
<a href="mailto:<?= $this->_user->email; ?>"><?= $this->_user->email; ?></a>
</div>Returning Content
Templates can also return values instead of echoing them. Luminova automatically executes callables and formats the returned data before sending it to the client.
Supported return types and behavior:
| Type | Output Handling |
|---|---|
Scalar (string, int, float, bool) | Converted to string and sent directly. |
| Array | JSON-encoded and sent with Content-Type: application/json. |
| Object | JSON-encoded with Content-Type: application/json (unless it implements __toString() or toString()). |
| Stringable Object | Converted to string via casting to (string) and sent. |
| SimpleXMLElement | Converted to XML via asXML() with Content-Type: application/xml. |
| DOMDocument | Converted to XML via saveXML() with Content-Type: application/xml. |
| Callable | Executed, and its return value is processed recursively using the same rules. |
Examples:
Returning an array.
// resources/Views/example.php
return [
'name' => $this->_user->name,
'email' => $this->_user->email
];Returning an object implementing __toString()
return new class($this->_user) {
public function __construct(private User $user) {}
public function __toString(): string {
return sprintf(
'Contact %s, via %s',
$this->user->name,
$this->user->email
);
}
};Returning a DOMDocument.
$dom = new DOMDocument();
$dom->loadXML('
<user>
<name>' . $this->_user->name . '</name>
<email>' . $this->_user->email . '</email>
</user>'
);
return $dom;Returning a SimpleXMLElement → sent as XML
$xml = new SimpleXMLElement('<user/>');
$xml->addChild('name', $this->_user->name);
$xml->addChild('email', $this->_user->email);
return $xml;Returning a callable.
return function(): array
{
return ['timestamp' => time()];
};Note:
Returning
arraysorobjectsautomatically produces JSON output to the client.Returningscalars,Stringableobjects, orXMLtypes are rendered according to their type.Callables are executed before output.
Class Definition
- Class namespace:
Luminova\Template\View - This class is marked as final and can't be subclassed
- This class implements:Luminova\Interface\LazyObjectInterface
Constants
Template Content Types
These constants define template content type identifiers used by the framework for operations such as caching, minification, and other processing. For more rendering support see Response Class Documentation
| Constant | Type | Value | Description |
|---|---|---|---|
HTML | string | 'html' | HTML templates or content. |
JSON | string | 'json' | JSON-formatted content. |
TEXT | string | 'txt' | Plain text content. |
XML | string | 'xml' | XML-formatted content. |
JS | string | 'js' | JavaScript files or code blocks. |
CSS | string | 'css' | CSS stylesheets or inline styles. |
RDF | string | 'rdf' | RDF-formatted content for semantic data. |
ATOM | string | 'atom' | ATOM feed format. |
RSS | string | 'rss' | RSS feed format. |
BIN | string | 'bin' | Binary string content format. |
Note:
They are not the same as HTTP header
Content-Typevalues, but are used internally for purposes such as caching, minification, and content processing.If no explicit HTTP header content type is set, these constants can also be used to determine the appropriate response header.
Properties
app
Holds the application instance.
public ?Luminova\Foundation\Core\Application $app = nullDoes not create a circular reference with the view instance.
Examples:
$this->app;
// or
$self->app;When to use:Use this property when you need access to application-level services, configuration, or methods directly from within a template without instantiating or passing the application object manually.Use
exportfor private properties.
Methods
constructor
Initialize the View object.
This constructor sets up template configuration for view management, and loads environment-based options.
public __construct(?Luminova\Foundation\Core\Application $app): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$app | CoreApplication|null | Optional application object. If omitted, templates will not have access to the application instance via ( $this->app or $self->app). |
Note: You may not need to initialize this class manually as it is already available within the
Luminova\Foundation\Core\Applicationclass, which can be accessed inside controller classes.For global helper access, useLuminova\Funcs\view(...).
view
Sets the view template and its content type for rendering.
It resolves the template path and prepares it for rendering or later access.
Call this method to specify which view file to use, before any of these methods (render(), contents(), promise(), exists() or info()) are called.
Search Paths:
/resources/Views/— MVC view directory./app/Modules/Views/— HMVC root view directory./app/Modules/<Module>/Views/— HMVC module-specific views.
Supported Content Types:
html,json,text|txt,xml,js,css,rdf,atom,rss
The
$templatemust exclude file extensions (.php,.tpl,.twg, etc).For unsupported types, useresponse(...)->render(...)for manual handling.
final public view(string $template, string $type = self::HTML): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$template | string | View filename without extension (e.g., dashboard/index). |
$type | string | The template content type (default: View::HTML). |
Return Value:
self - Return instance of template view class.
Throws:
- Luminova\Exceptions\InvalidArgumentException - If
$typeis not a supported view type.
Example:
Usage in Controller:
// /app/Controllers/Http/FooController.php
// Render view and return status
$status = $this->tpl->view('profile', View::HTML)
->render(['id' => 1]);
// Render view and return content
$content = $this->tpl->view('settings', View::JSON)
->contents(['tab' => 'privacy']);
// Render view and return promise object
$promise = $this->tpl->view('invoice', View::HTML)
->promise(['orderId' => 101]);
// Return template filesystem information
$info = $this->tpl->view('index', View::HTML)
->info();Direct Usage:
$view = new View(app: $application);render
Render and immediately send the view output.
This method renders view content with an optional parameters to make globally availablewithin the template view file.
final public render(array<string,mixed> $options = [], int $status = 200): intParameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<string,mixed> | Additional parameters to pass in the template (available inside view). |
$status | int | The HTTP status code (default: 200 OK). |
Return Value:
int - Return one of the following status codes:
STATUS_SUCCESSif the view is handled successfully,STATUS_SILENCEif failed, silently terminate without error page allowing you to manually handle the state.
Throws:
- Luminova\Exceptions\RuntimeException - If the view rendering fails.
Example:
Display template view with options:
// /app/Controllers/Http/FooController.php
public function fooView(): int
{
return $this->tpl->view('name')
->render([...], 200);
}contents
Render the view and return the output as a string.
This method renders selected template view and return the rendered contents string.
final public contents(array<string,mixed> $options = [], int $status = 200): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<string,mixed> | Additional parameters to pass in the template (available inside view). |
$status | int | HTTP status code (default: 200 OK). |
Return Value:
string|null - Return the compiled view contents or null if no content.
Throws:
- Luminova\Exceptions\RuntimeException - If the view rendering fails.
Example:
Display your template view or send as an email:
// /app/Controllers/Http/MailController.php
use Luminova\Utility\Email\Mailer;
public function mailView(): int
{
$content = $this->tpl->view('name')
->contents(['foo' => 'bar'], 200);
Mailer::to('[email protected]')->send($content);
}promise
Return a promise that resolves with rendered view contents.
Renders the template view file and returns a promise that resolves with the rendered contents, or rejects if the template is missing or rendering fails.
final public promise(array<string,mixed> $options = [], int $status = 200): PromiseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$options | array<string,mixed> | Additional parameters to pass in the template (available inside view). |
$status | int | HTTP status code (default: 200 OK). |
Return Value:
Luminova\Interface\PromiseInterface - Return a promise that resolves with rendered view contents or rejects with an error.
Example:
Display your template view or send as an email:
// /app/Controllers/Http/FooController.php
public function fooView(): int
{
$content = $this->tpl->view('name')
->promise(['foo' => 'bar'])
->then(function(string $content, array $options) {
Mailer::to('[email protected]')->send($content);
})->catch(function(Exception $e) {
logger('debug', $e->getMessage());
});
}info
Returns metadata about the specified view template.
Provides useful diagnostic and contextual information about a template file without rendering.
final public info(?string $key = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string|null | Optional key to retrieve a specific value. |
Return Value:
array<string,mixed>|mixed - Return the value for that key (null if not found), otherwise return metadata array.
Example:
$info = $this->tpl->view('dashboard')
->info();
// Get the modified
$modified = $this->tpl->view('dashboard')
->info('modified');exists
Check if a template view file exists.
final public exists(): boolReturn Value:
bool - Return true if the template view file exists, false otherwise.
Example:
if($this->tpl->view('admin')->exists()){
// Yes
}export
Export a class instance or fully qualified name for later access in templates.
This allows registering services, classes, or any custom objectso that it can be accessed in the template via its alias.
Alternatively you can access public/protected properties and methods in application class directly using
$this->app->propertyor$self->app->property.
final public export(object|string $target, ?string $alias = null, bool $shared = false): trueParameters:
| Parameter | Type | Description |
|---|---|---|
$target | object|string | The class name, object instance to expose. |
$alias | string|null | Optional alias for reference (Defaults to class class/object name). |
$shared | bool | If true and $target is class name, the same instance will be reused. |
Return Value:
bool - Returns true if imported, otherwise throw error.
Throws:
- Luminova\Exceptions\RuntimeException - If arguments are invalid or alias already exists.
Example:
// /app/Application.php
class Application extends CoreApplication
{
protected function onCreate(): void
{
$session = new Session();
$session->setStorage('users');
$session->start();
// Export the session object for use in views
$this->view->export($session, 'session');
}
}Note:If
$targetis not an object, it’s treated as a class name that will be instantiated later.
Accessing Exported Data in Views
Exported properties can be accessed using their $alias.If no alias is provided, the class basename is used automatically.
// /resources/Views/example.php
$this->session->isOnline();
// or when using isolated mode
$self->session->isOnline();You can also retrieve exports through the helper method:
// /resources/Views/example.php
$this->getExport('session');
// or
$self->getExport('session');header
Set a single response header.
This allows you to add a response header for template rendering,
final public header(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The template response header key. |
$value | mixed | The header value for key. |
Return Value:
self - Return instance of template view class.
Example:
$this->tpl->header('Content-Type', 'application/json');headers
Set multiple response headers at once.
This allows you to add a custom response headers for template rendering,
final public headers(array<string,mixed> $headers): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$headers | array<string,mixed> | Associative array of headers key-pair. |
Return Value:
self - Return instance of template view class.
Example:
$this->tpl->headers([
'Cache-Control' => 'no-cache',
'Content-Type' => 'text/html; charset=utf-8',
]);setModule
Sets the HMVC module name for the current controller class.
This identifies the module that the controller belongs to, typically matching the folder name under app/Modules/<Module>.
For example, a controller located atapp/Modules/Blog/Controllers/PostController.php should use 'Blog' as the module name.
Use an empty string if the controller is not part of root module (i.e., global scope).
final public setModule(string $module = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$module | string | The module name (e.g., 'Blog'). Must not contain slashes or backslashes. |
Return Value:
self - Return instance of template view class.
Throws:
- Luminova\Exceptions\RuntimeException - If the module name contains invalid characters (e.g., slashes).
Example:
// /app/Modules/Blog/Controllers/Http/ExampleController.php
namespace App\Modules\Blog\Controllers\Http;
use Luminova\Base\Controller;
class ExampleController extends Controller
{
protected function onCreate(): void
{
$this->tpl->setModule('Blog');
}
//...
}Note:
This method is intended for HMVC design pattern only, and should be called once in the controller’s
__constructoronCreatemethod—before rendering any views.
setFolder
Sets the view subfolder used to locate view files within the application template view directory.
Valid base locations include:
resources/Views/- MVC View directory.app/Modules/Views/- HMVC root view directory.app/Modules/<Module>/Views/- HMVC custom module view directory.
final public setFolder(string $path): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Subfolder name to look for views. |
Return Value:
self - Return instance of template view class.
Notes:
- When used in a controller's
onCreateor__construct, all views for that controller will be searched in this folder.- When used in the application's
onCreateor__construct, it sets the default folder for all views.- When used in a controller method before rendering, only that method's view resolution is affected.
Example:
In this example, the setFolder method is used to direct the framework to look for template files in a custom directory (resources/Views/example/) for all views within a specific controller.
// /app/Controllers/Http/ExampleController.php
namespace App\Controllers\Http;
use Luminova\Base\Controller;
class ExampleController extends Controller
{
protected function onCreate(): void
{
$this->tpl->setFolder('example');
}
public function show(): int
{
return $this->view('show');
}
public function foo(): int
{
// Overrides the default folder for the current method.
$this->tpl->setFolder('example2');
return $this->view('foo');
}
}setUriPathDepth
Set URI relative parent directories depth.
This method allows you to manually set how many parent directories (../) to prepend to asset and link paths.
It overrides Luminova's default auto-detection based on the current URI depth.Use it when working with nested views or custom routes that require explicit relative path control.
Example depth values:
1→../2→../../
final public setUriPathDepth(int $depth): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$depth | int | Number of ../ segments to prepend. |
Return Value:
self - Return instance of template view class.
Useful when custom routing or nested views affect the correct relative path for assets.
Example:
Force asset paths to go up one directory:
// /app/Controllers/Http/ExampleController.php
namespace App\Controllers\Http;
use Luminova\Base\Controller;
class ExampleController extends Controller
{
#[Route('app/account', methods: ['GET'])]
protected function example(): void
{
// Override auto depth detection to use only one "../"
$this->tpl->setUriPathDepth(1);
return $this->view('account');
}
}In the corresponding view file:
// /resources/Views/account.php
echo Luminova\Funcs\asset('images/logo.png');
// OR
echo "{$this->_asset}images/logo.png";
// Generates: ../assets/images/logo.png
echo Luminova\Funcs\href('account/foo');
// OR
echo "{$this->_href}account/foo";
// Generates: ../account/fooNormally, accessing
https://example.com/app/accountwould trigger auto-detection of../../in development environment likeXAMPPdue to the URI depth (public/app/account).But since we explicitly set the depth to1, only a single../is prepended to asset and link paths.
codeblock
HTML codeblock minification behavior.
This method allows you to configure whether HTML <code> blocks should be excluded from minificationand optionally display a copy button.
final public codeblock(bool $minify, bool $button = false): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$minify | bool | Whether to skip minifying <code> blocks. |
$button | bool | Whether to show a "copy" button inside code blocks (default: false). |
Return Value:
self - Return instance of template view class.
noCaching
Exclude specific templates from being cached.
By default when page.caching = true is enabled in env, all view templates are cached and reused.This method allows you to exclude one or more templates name from caching it rendered content. Useful for selectively exclusion, ensuring that dynamic content is always up-to-date.
final public noCaching(string|string[] $template): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$template | string|string[] | A single template name or an array of template names to ignore from caching. |
Return Value:
self - Return instance of template view class.
Example:
To disable caching for specific a view templates, example insert and edit will be excluded while allowing cache for other templates.
// /app/Application.php
namespace App;
use Luminova\Foundation\Core\Application as CoreApplication;
class Application extends CoreApplication
{
protected function onCreate(): void
{
$this->view->noCaching(['insert', 'edit']);
}
}This can be configured either in your
routes/<context>.phpfile,Applicationclass or your viewControllerclass.The recommended approach is within the applicationonCreateor__constructmethod.
cacheOnly
Specify templates that should be cached exclusively.
This method allows you to explicitly add one or more templates that should only be cached.
Unlike noCaching method, the cacheOnly method allows you to specify which view templates should be exclusively cached while others are ignored. This is useful when you want to ensure that only certain views are cache and excluded others entirely.
final public cacheOnly(string|string[] $template): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$template | string|string[] | A single template name or an array of template names to allow for caching. |
Return Value:
self - Return instance of template view class.
Example:
Enable caching for only home, about, and contact views
// /app/Application.php
namespace App;
use Luminova\Foundation\Core\Application as CoreApplication;
class Application extends CoreApplication
{
protected function onCreate(): void
{
$this->view->cacheOnly(['home', 'about', 'contact']);
}
}Applicable Usage:
If you have
100views but only want to cache three of them (e.g,home,about, andcontact). UsingnoCachingwould mean listing all the other97views.Instead, usecacheOnlyto list just the views you want cached. This way, only those three are cached, and everything else is automatically excluded.
cacheable
Enable or disable view caching globally at the controller or application level.
final public cacheable(bool $cacheable = true): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$cacheable | bool | Whether to allow view caching. |
Return Value:
self - Return instance of template view class.
If used in a controller’s
onCreate()or__construct(), all views under that controller will follow this rule.If used in the application class, applies to all views globally.Useful in api context to manually handle caching.
Example:
The cacheable method can be called in the controller's or application's onCreate or __construct method.
// /app/Application.php
namespace App\Controllers\Http;
use Luminova\Foundation\Core\Application as CoreApplication;
class Application extends CoreApplication
{
protected function onCreate(): void
{
$this->view->cacheable(false);
}
}The
cacheablemethod provides a straightforward way to control view caching by setting the caching status globally or for specific controllers, you can ensure that dynamic content remains current without being cached.
cache
Enables view response caching for reuse on future requests.
When called, this method marks the response to be cached. You can optionallyspecify a custom expiration time; otherwise, the default from the .env config will be used.
final public cache(\DateTimeInterface|int|null $expiry = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$expiry | \DateTimeInterface|int|null | Optional cache expiration. Accepts: - DateTimeInterface for specific expiration date/time,- int for duration in seconds,- null to use the default expiration from configuration. |
Return Value:
self - Return instance of template view class.
Example:
Basic usage with conditional caching.
// /app/Controllers/Http/FooController.php
public function fooView(Luminova\Base\Model $model): int
{
$cache = $this->tpl->cache(60); // Cache for 60 seconds
if ($cache->expired()) {
$heavy = $model->doHeavyProcess();
return $cache->view('foo')
->render(['data' => $heavy]);
}
return $cache->reuse(); // Reuse the previously cached response
}delete
Deletes the cache entry for the current request view.
final public delete(?string $version = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$version | string|null | Optional. Specify the application version to delete (default: null). |
Return Value:
bool - Return true if the cache entry was deleted; false otherwise.
clear
Clears all view cache entries.
final public clear(?string $version = null): intParameters:
| Parameter | Type | Description |
|---|---|---|
$version | string|null | Optional. Specify the application version to clear (default: null). |
Return Value:
int - Return the number of deleted cache entries.
Example:
Clear all cache entries for the current application version.
$total = $this->tpl->clear();Clear cache entries for a specific application version.
$total = $this->tpl->clear('1.0.0');expired
Check if page cache has expired.
Note: the expiration check we use the time used while saving cache.
final public expired(?string $type = self::HTML): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$type | string|null | The view content extension type (default: self::HTML). |
Return Value:
bool - Returns true if cache doesn't exist or expired.
Throws:
- Luminova\Exceptions\RuntimeException - Throw if the cached version doesn't match with the current view type.
Note:
Before calling this method, you must first call
cachemethod instantiate it for use.For the expiration check, we use the expiration set in
envwhile saving cache, also don't worry about the cache key as it handled internally to save your precious time.
reuse
Reuse previously cached view content if available.
final public reuse(): intReturn Value:
int - Returns one of the following status codes:
STATUS_SUCCESSif cache was found and successfully reused,STATUS_SILENCEif no valid cache was found — silent exit, allowing manual fallback logic.
Throws:
- Luminova\Exceptions\RuntimeException - If called without first calling
cache()method.
Example:
public function homepage(Luminova\Base\Model $model): int
{
// Enable caching for 2 minutes
$this->tpl->cache(120);
if ($this->tpl->reuse() === STATUS_SUCCESS) {
// Cache hit, response already sent
return STATUS_SUCCESS;
}
$data = $model->getHomepageData();
return $this->tpl->view('home')
->render(['data' => $data]);
}onExpired
Conditionally renew cached view if expired, otherwise reuse it.
final public onExpired(Closure $onRenew, array $options = [], string $type = View::HTML): intParameters:
| Parameter | Type | Description |
|---|---|---|
$onRenew | \Closure | Callback to execute if cache has expired. Should return STATUS_SUCCESS or STATUS_SILENCE. |
$options | array | Optional options to pass to the $onRenew callback argument. |
$type | string | Type of content to check cache for, (e.g. View::HTML, View::JSON). |
Return Value:
int - Return the status code:
- Status code from the callback if cache is expired or bypassed,
- Otherwise, status code from
reuse()if valid cache is used.
Example:
public function books(): int
{
return $this->tpl->cache(300)
->onExpired(function(array $options): int {
return $this->view('books', [
'group' => $options['group'],
'books' => [
['id' => 100, 'name' => 'Basic Programming'],
['id' => 1001, 'name' => 'Advanced Programming']
]
]);
},
['group' => 'programming']
);
}redirect
Redirect to a different URI or route.
final public redirect(string $uri, int $status = 302): neverParameters:
| Parameter | Type | Description |
|---|---|---|
$uri | string | The target URI or route. |
$status | int | The HTTP redirect status code (default: 302). |
link
Generate a relative URI from the public root directory.
This method creates a relative path for routes or public assets (e.g., CSS, JS, images)starting from the controller’s public directory. In production, it returns a root-relative path.In development, it calculates the relative path based on URI segments.
final public static link(string $route = '', ?int $depth = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$route | string | Optional route or file path to append after the base path. |
$depth | int|null | Optional depth to parent directory (used in development mode only). If null, the method auto-detects the depth. |
Return Value:
string - Return a relative or root-based URL to the file or route.
Example:
<link href="<?= $this->link('assets/css/main.css') ?>" rel="stylesheet">
<a href="<?= $this->link('about') ?>">About Us</a>hasOption
Checks if a given name exists as a view option.
Useful for determining whether a value was set in the current view context via configuration options.
final public hasOption(string $name): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The option name to check. |
Return Value:
bool - Returns true if the option exists, otherwise false.
isExported
Checks if a given property exists as an exported alias.
Useful for determining whether a value was made available to thecurrent view via the export() method.
final public isExported(string $name): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The export name or alias to check. |
Return Value:
bool - Returns true if the export exists, otherwise false.
getExport
Retrieves a property exported from the application context.
final public getExport(string $name): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The export property class name or alias used when exporting. |
Return Value:
mixed|null - Return the export value, or null if not found.
getOption
Retrieves a value from the view's context options.
final public getOption(string $key): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The option name. |
Return Value:
mixed|null - Returns the option value if set; otherwise, checks for the key prefixed with _ and returns that value if found. Returns null if neither exists.
Note:
Supports fallback to prefixed keys for flexibility and backward compatibility.
getExports
Returns all exported application properties available to the view.
final public getExports(): array<string,mixed>Return Value:
array<string,mixed> - Return an associative array of exported properties.
getOptions
Returns all view context options extracted for the view.
final public getOptions(): array<string,mixed>Return Value:
array<string,mixed> - Return an associative array of view context options.
getTemplate
Get the template filename.
By default, returns the resolved template name (which may differ from the originalif a fallback like 404 was used).
Pass false to get the original template name exactly as specified in the controller.
final public getTemplate(bool $resolved = true): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$resolved | bool | Whether to return the resolved filename (true) or the original (false). |
Return Value:
string - Return the current view template file basename.
getStatusCode
Get the current HTTP status code.
Useful when you need to access the same status code that was used to render the template directly from inside the template itself.
final public getStatusCode(): stringReturn Value:
int - Return the HTTP status code (e.g., 200, 404, 500).
toTitle
Converts the current view template name to a formatted page title.
Replaces underscores, dashes, hyphens, and commas with spaces, capitalizes words,and optionally appends the application name as a suffix.
final public toTitle(bool $suffix = false): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$suffix | bool | Whether to append the app name as a suffix (default: false). |
Return Value:
string - Return the formatted page title.