Neuron-PHP

Base
in package
implements IController

Table of Contents

Interfaces

IController
Controller interface defining the contract for MVC controllers.

Properties

$_app  : IMvcApplication
$_router  : Router|null

Methods

__call()  : string|null
Magic method to provide Rails-style URL helper methods in controllers.
__construct()  : mixed
getApplication()  : IMvcApplication
getRouter()  : Router
register()  : void
This method registers routes for any of the standard methods that are currently implemented in the class.
renderHtml()  : string
Renders an HTML response.
renderHtmlWithCacheKey()  : string
Render HTML with separate cache key data.
renderJson()  : string
Renders a JSON response.
renderMarkdown()  : string
Renders a Markdown response.
renderMarkdownWithCacheKey()  : string
Render Markdown with separate cache key data.
renderXml()  : string
Renders an XML response.
setApplication()  : Base
setRouter()  : Base
getControllerName()  : string
Get the controller name for cache key generation.
getControllerViewPath()  : string
Get the controller view path accounting for namespace hierarchy.
getViewCache()  : string|null
Get cached view content if available.
getViewCacheByKey()  : string|null
Get cached view content using only cache key data.
hasViewCache()  : bool
Check if a view cache exists for the given page and data.
hasViewCacheByKey()  : bool
Check if a view cache exists using only cache key data.
initializeViewCache()  : ViewCache|null
Initialize ViewCache if not already present in Registry.
injectHelpers()  : array<string|int, mixed>
Inject URL helpers and other view helpers into view data.
isCacheEnabledByDefault()  : bool
Check if cache is enabled by default in system settings.
registerAdd()  : void
registerCreate()  : void
registerDelete()  : void
registerEdit()  : void
registerIndex()  : void
registerShow()  : void
registerUpdate()  : void
routeExists()  : bool
Check if a named route exists.
urlFor()  : string|null
Generate a relative URL for a named route.
urlForAbsolute()  : string|null
Generate an absolute URL for a named route.
urlHelper()  : UrlHelper|null
Create a new UrlHelper instance for use in controllers.
view()  : ViewContext
Create a new fluent ViewContext for building and rendering views.
storeCachedView()  : void
Store rendered content in view cache.

Properties

Methods

__call()

Magic method to provide Rails-style URL helper methods in controllers.

public __call(string $method, array<string|int, mixed> $arguments) : string|null

Supports patterns like:

  • $this->userProfilePath(['id' => 123]) -> generates relative URL for 'user_profile' route
  • $this->userProfileUrl(['id' => 123]) -> generates absolute URL for 'user_profile' route
Parameters
$method : string

The method name (e.g., 'userProfilePath', 'userProfileUrl')

$arguments : array<string|int, mixed>

Method arguments, first should be parameters array

Tags
throws
BadMethodCallException

If a method pattern is not recognized

example
// In a controller method:
$profilePath = $this->userProfilePath(['id' => $userId]);
$absoluteUrl = $this->userProfileUrl(['id' => $userId]);

// Redirect to a named route:
return redirect($this->userDashboardPath());
Return values
string|null

Generated URL or null if route not found

register()

This method registers routes for any of the standard methods that are currently implemented in the class.

public static register(IMvcApplication $app[, string $route = '' ]) : void

Index Add Show Create Edit Update Delete

Parameters
$app : IMvcApplication
$route : string = ''
Tags
throws
BadRequestMethod

renderHtml()

Renders an HTML response.

public renderHtml(HttpResponseStatus $responseCode[, array<string|int, mixed> $data = [] ][, string $page = "index" ][, string $layout = "default" ][, bool|null $cacheEnabled = null ]) : string
Parameters
$responseCode : HttpResponseStatus
$data : array<string|int, mixed> = []
$page : string = "index"
$layout : string = "default"
$cacheEnabled : bool|null = null
Tags
throws
NotFound
Return values
string

renderHtmlWithCacheKey()

Render HTML with separate cache key data.

public renderHtmlWithCacheKey(HttpResponseStatus $responseCode[, array<string|int, mixed> $viewData = [] ][, array<string|int, mixed> $cacheKeyData = [] ][, string $page = "index" ][, string $layout = "default" ][, bool|null $cacheEnabled = null ]) : string

Allows checking/using cache without fetching full view data.

Parameters
$responseCode : HttpResponseStatus

HTTP response status

$viewData : array<string|int, mixed> = []

Full data for rendering (can be empty if using cache)

$cacheKeyData : array<string|int, mixed> = []

Minimal data for cache key generation

$page : string = "index"

Page template name

$layout : string = "default"

Layout template name

$cacheEnabled : bool|null = null

Whether to enable caching

Tags
throws
NotFound
Return values
string

Rendered HTML content

renderJson()

Renders a JSON response.

public renderJson(HttpResponseStatus $responseCode[, array<string|int, mixed> $data = [] ]) : string
Parameters
$responseCode : HttpResponseStatus
$data : array<string|int, mixed> = []
Return values
string

renderMarkdown()

Renders a Markdown response.

public renderMarkdown(HttpResponseStatus $responseCode[, array<string|int, mixed> $data = [] ][, string $page = "index" ][, string $layout = "default" ][, bool|null $cacheEnabled = null ]) : string
Parameters
$responseCode : HttpResponseStatus
$data : array<string|int, mixed> = []
$page : string = "index"
$layout : string = "default"
$cacheEnabled : bool|null = null
Tags
throws
CommonMarkException
throws
NotFound
Return values
string

renderMarkdownWithCacheKey()

Render Markdown with separate cache key data.

public renderMarkdownWithCacheKey(HttpResponseStatus $responseCode[, array<string|int, mixed> $viewData = [] ][, array<string|int, mixed> $cacheKeyData = [] ][, string $page = "index" ][, string $layout = "default" ][, bool|null $cacheEnabled = null ]) : string

Allows checking/using cache without fetching full view data.

Parameters
$responseCode : HttpResponseStatus

HTTP response status

$viewData : array<string|int, mixed> = []

Full data for rendering (can be empty if using cache)

$cacheKeyData : array<string|int, mixed> = []

Minimal data for cache key generation

$page : string = "index"

Page template name

$layout : string = "default"

Layout template name

$cacheEnabled : bool|null = null

Whether to enable caching

Tags
throws
NotFound
throws
CommonMarkException
Return values
string

Rendered Markdown content

renderXml()

Renders an XML response.

public renderXml(HttpResponseStatus $responseCode[, array<string|int, mixed> $data = [] ]) : string
Parameters
$responseCode : HttpResponseStatus
$data : array<string|int, mixed> = []
Return values
string

getControllerName()

Get the controller name for cache key generation.

protected getControllerName() : string

Returns the short class name (without namespace). This matches how the framework's render methods set the controller name.

Return values
string

The controller class name without namespace

getControllerViewPath()

Get the controller view path accounting for namespace hierarchy.

protected getControllerViewPath() : string

Converts controller namespace and class name to snake_case directory structure.

Examples:

  • Neuron\Cms\Controllers\Admin\Posts -> admin/posts
  • Neuron\Cms\Controllers\PostController -> post (backwards compatible with "Controller" suffix)
  • Neuron\Cms\Controllers\Dashboard -> dashboard
Return values
string

The view path (e.g., "admin/posts", "dashboard")

getViewCache()

Get cached view content if available.

protected getViewCache(string $page[, array<string|int, mixed> $data = [] ]) : string|null

Initializes ViewCache if needed.

Parameters
$page : string

The page/view name

$data : array<string|int, mixed> = []

The data that affects cache key generation

Return values
string|null

The cached content or null if not found

getViewCacheByKey()

Get cached view content using only cache key data.

protected getViewCacheByKey(string $page[, array<string|int, mixed> $cacheKeyData = [] ]) : string|null

This allows retrieving the cache without fetching full view data.

Parameters
$page : string

The page/view name

$cacheKeyData : array<string|int, mixed> = []

The minimal data that determines cache uniqueness

Return values
string|null

The cached content or null if not found

hasViewCache()

Check if a view cache exists for the given page and data.

protected hasViewCache(string $page[, array<string|int, mixed> $data = [] ]) : bool

Initializes ViewCache if needed.

Parameters
$page : string

The page/view name

$data : array<string|int, mixed> = []

The data that affects cache key generation

Return values
bool

True if a cache exists, false otherwise

hasViewCacheByKey()

Check if a view cache exists using only cache key data.

protected hasViewCacheByKey(string $page[, array<string|int, mixed> $cacheKeyData = [] ]) : bool

This allows checking cache without fetching full view data.

Parameters
$page : string

The page/view name

$cacheKeyData : array<string|int, mixed> = []

The minimal data that determines cache uniqueness

Return values
bool

True if cache exists, false otherwise

initializeViewCache()

Initialize ViewCache if not already present in Registry.

protected initializeViewCache() : ViewCache|null

This allows controllers to check cache before making expensive API calls.

Return values
ViewCache|null

The ViewCache instance or null if initialization fails

injectHelpers()

Inject URL helpers and other view helpers into view data.

protected injectHelpers(array<string|int, mixed> $data) : array<string|int, mixed>

This method:

  1. Merges global view data from ViewDataProvider (if registered)
  2. Injects UrlHelper for route generation (if router available)
  3. Controller-specific data takes precedence over global data
Parameters
$data : array<string|int, mixed>

The view data array

Return values
array<string|int, mixed>

Data array with helpers and global data injected

isCacheEnabledByDefault()

Check if cache is enabled by default in system settings.

protected isCacheEnabledByDefault() : bool
Return values
bool

True if cache is enabled in settings, false otherwise

registerAdd()

protected static registerAdd(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerCreate()

protected static registerCreate(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerDelete()

protected static registerDelete(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerEdit()

protected static registerEdit(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerIndex()

protected static registerIndex(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerShow()

protected static registerShow(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

registerUpdate()

protected static registerUpdate(Application $app, string $controller, string $route) : void
Parameters
$app : Application
$controller : string
$route : string
Tags
throws
BadRequestMethod

routeExists()

Check if a named route exists.

protected routeExists(string $routeName) : bool
Parameters
$routeName : string

The route name to check

Return values
bool

True if route exists, false otherwise

urlFor()

Generate a relative URL for a named route.

protected urlFor(string $routeName[, array<string|int, mixed> $parameters = [] ][, string|null $fallback = null ]) : string|null
Parameters
$routeName : string

The name of the route

$parameters : array<string|int, mixed> = []

Parameters to substitute in the route path

$fallback : string|null = null

Fallback URL if route not found

Return values
string|null

The generated relative URL, fallback if provided, or null if route not found

urlForAbsolute()

Generate an absolute URL for a named route.

protected urlForAbsolute(string $routeName[, array<string|int, mixed> $parameters = [] ][, string|null $fallback = null ]) : string|null
Parameters
$routeName : string

The name of the route

$parameters : array<string|int, mixed> = []

Parameters to substitute in the route path

$fallback : string|null = null

Fallback URL if route not found

Return values
string|null

The generated absolute URL, fallback if provided, or null if route not found

urlHelper()

Create a new UrlHelper instance for use in controllers.

protected urlHelper() : UrlHelper|null
Return values
UrlHelper|null

The URL helper instance or null if router not available

view()

Create a new fluent ViewContext for building and rendering views.

protected view() : ViewContext

This provides a clean, chainable API for constructing view data and rendering.

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

// With auto-injection
return $this->view()
    ->title('Profile')
    ->withCurrentUser()
    ->withCsrfToken()
    ->render('profile');
Return values
ViewContext

A new view context instance

storeCachedView()

Store rendered content in view cache.

private storeCachedView(string $page, array<string|int, mixed> $cacheKeyData, string $content, bool|null $cacheEnabled) : void

Handles cache initialization, temporary enablement, and error handling.

Parameters
$page : string

The page/view name

$cacheKeyData : array<string|int, mixed>

Data for cache key generation

$content : string

The rendered content to cache

$cacheEnabled : bool|null

Whether caching is explicitly enabled


        
On this page

Search results