Blog
extends Content
in package
Base content controller for the Neuron CMS framework.
This abstract controller provides foundational functionality for all CMS content types, including site configuration management, version tracking, SEO metadata handling, and common rendering operations. It serves as the base class for specialized controllers like Blog, Pages, and other content types.
Key features:
- Site configuration and metadata management
- Version information loading and tracking
- RSS feed URL configuration and management
- Registry-based settings integration
- Fluent interface for property configuration
- Markdown rendering support for content pages
- SEO-friendly title and description handling
- Base URL and canonical URL management
The controller automatically loads site settings from the registry and configures common CMS properties, making it easy for derived controllers to focus on content-specific functionality while inheriting consistent site-wide configuration and behavior.
Attributes
- #[RouteGroup]
- $prefix: '/blog'
Table of Contents
Properties
- $_sessionManager : SessionManager
- $_settings : SettingManager
- $_app : IMvcApplication
- $_categoryRepository : ICategoryRepository
- $_description : string
- $_name : string
- $_postRepository : IPostRepository
- $_renderer : EditorJsRenderer
- $_router : Router|null
- $_rssUrl : string
- $_tagRepository : ITagRepository
- $_title : string
- $_url : string
- $_userRepository : IUserRepository
Methods
- __call() : string|null
- Magic method to provide Rails-style URL helper methods in controllers.
- __construct() : mixed
- author() : string
- Blog posts by author
- category() : string
- Blog posts by category
- feed() : string
- Generate RSS feed
- getApplication() : IMvcApplication
- getDescription() : string
- getName() : string
- getRouter() : Router
- getRssUrl() : string
- Getters and Setters
- getTitle() : string
- getUrl() : string
- index() : string
- Blog homepage - list of published posts
- markdown() : string
- Render a markdown page.
- 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
- setDescription() : $this
- setName() : $this
- setRouter() : Base
- setRssUrl() : Content
- Set the RSS URL for the site.
- setTitle() : $this
- setUrl() : Content
- Set the URL for the site.
- show() : string
- Blog article detail view
- tag() : string
- Blog posts by tag
- createDto() : Dto
- Create a DTO from a YAML configuration file.
- flash() : void
- Set a flash message for the next request.
- getControllerName() : string
- Get the controller name for cache key generation.
- getControllerViewPath() : string
- Get the controller view path accounting for namespace hierarchy.
- getSessionManager() : SessionManager
- Get the session manager and ensure session is started.
- 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.
- initializeCsrfToken() : void
- Initialize CSRF token and store in Registry for template access.
- 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.
- mapRequestToDto() : void
- Map HTTP request data to a DTO.
- redirect() : never
- Redirect to a named route with optional flash message.
- redirectBack() : never
- Redirect back to the previous page or a fallback URL.
- redirectToUrl() : never
- Redirect to a URL path with optional flash message.
- 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.
- validationError() : never
- Handle validation errors by redirecting with flash message.
- view() : ViewContext
- Create a new fluent ViewContext for building and rendering views.
- storeCachedView() : void
- Store rendered content in view cache.
Properties
$_sessionManager
protected
SessionManager
$_sessionManager
$_settings
protected
SettingManager
$_settings
$_app
private
IMvcApplication
$_app
$_categoryRepository
private
ICategoryRepository
$_categoryRepository
$_description
private
string
$_description
= ''
$_name
private
string
$_name
= 'Blahg'
$_postRepository
private
IPostRepository
$_postRepository
$_renderer
private
EditorJsRenderer
$_renderer
$_router
private
Router|null
$_router
= null
$_rssUrl
private
string
$_rssUrl
= 'example.com/blog/rss'
$_tagRepository
private
ITagRepository
$_tagRepository
$_title
private
string
$_title
= 'Blahg'
$_url
private
string
$_url
= 'example.com/bog'
$_userRepository
private
IUserRepository
$_userRepository
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
Return values
string|null —Generated URL or null if route not found
__construct()
public
__construct(IMvcApplication $app, SettingManager $settings, SessionManager $sessionManager, IPostRepository $postRepository, ICategoryRepository $categoryRepository, ITagRepository $tagRepository, IUserRepository $userRepository, EditorJsRenderer $renderer) : mixed
Parameters
- $app : IMvcApplication
- $settings : SettingManager
- $sessionManager : SessionManager
- $postRepository : IPostRepository
- $categoryRepository : ICategoryRepository
- $tagRepository : ITagRepository
- $userRepository : IUserRepository
- $renderer : EditorJsRenderer
Tags
author()
Blog posts by author
public
author(Request $request) : string
Parameters
- $request : Request
Tags
Attributes
- #[Get]
- '/author/:username'
- $name: 'blog_author'
Return values
stringcategory()
Blog posts by category
public
category(Request $request) : string
Parameters
- $request : Request
Tags
Attributes
- #[Get]
- '/category/:slug'
- $name: 'blog_category'
Return values
stringfeed()
Generate RSS feed
public
feed(Request $request) : string
Parameters
- $request : Request
Attributes
- #[Get]
- '/rss'
- $name: 'rss_feed'
- #[NoReturn]
Return values
stringgetApplication()
public
getApplication() : IMvcApplication
Return values
IMvcApplicationgetDescription()
public
getDescription() : string
Return values
stringgetName()
public
getName() : string
Return values
stringgetRouter()
public
getRouter() : Router
Return values
RoutergetRssUrl()
Getters and Setters
public
getRssUrl() : string
Return values
stringgetTitle()
public
getTitle() : string
Return values
stringgetUrl()
public
getUrl() : string
Return values
stringindex()
Blog homepage - list of published posts
public
index(Request $request) : string
Parameters
- $request : Request
Tags
Attributes
- #[Get]
- '/'
- $name: 'blog'
Return values
stringmarkdown()
Render a markdown page.
public
markdown(Request $request) : string
Parameters
- $request : Request
Tags
Return values
stringregister()
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
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
Return values
stringrenderHtmlWithCacheKey()
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
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
stringrenderMarkdown()
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
Return values
stringrenderMarkdownWithCacheKey()
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
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
stringsetApplication()
public
setApplication(IMvcApplication $app) : Base
Parameters
- $app : IMvcApplication
Return values
BasesetDescription()
public
setDescription(string $description) : $this
Parameters
- $description : string
Return values
$thissetName()
public
setName(string $name) : $this
Parameters
- $name : string
Return values
$thissetRouter()
public
setRouter(Router|null $router) : Base
Parameters
- $router : Router|null
Return values
BasesetRssUrl()
Set the RSS URL for the site.
public
setRssUrl(string $rssUrl) : Content
Parameters
- $rssUrl : string
Return values
ContentsetTitle()
public
setTitle(string $title) : $this
Parameters
- $title : string
Return values
$thissetUrl()
Set the URL for the site.
public
setUrl(string $url) : Content
Parameters
- $url : string
Return values
Contentshow()
Blog article detail view
public
show(Request $request) : string
Parameters
- $request : Request
Tags
Attributes
- #[Get]
- '/post/:slug'
- $name: 'blog_post'
Return values
stringtag()
Blog posts by tag
public
tag(Request|null $request) : string
Parameters
- $request : Request|null
Tags
Attributes
- #[Get]
- '/tag/:slug'
- $name: 'blog_tag'
Return values
stringcreateDto()
Create a DTO from a YAML configuration file.
protected
createDto(string $config) : Dto
Parameters
- $config : string
-
Path to YAML config file relative to Dtos/ (e.g., 'auth/login-request.yaml')
Tags
Return values
Dtoflash()
Set a flash message for the next request.
protected
flash(string $type, string $message) : void
Parameters
- $type : string
-
Message type (success, error, warning, info)
- $message : string
-
The message text
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")
getSessionManager()
Get the session manager and ensure session is started.
protected
getSessionManager() : SessionManager
Return values
SessionManagergetViewCache()
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
initializeCsrfToken()
Initialize CSRF token and store in Registry for template access.
protected
initializeCsrfToken() : void
Should be called by controllers that render forms requiring CSRF protection.
Note: Registry is used here as a view data bag to make CSRF tokens available to templates.
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:
- Merges global view data from ViewDataProvider (if registered)
- Injects UrlHelper for route generation (if router available)
- 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
mapRequestToDto()
Map HTTP request data to a DTO.
protected
mapRequestToDto(Dto $dto, Request $request) : void
Parameters
redirect()
Redirect to a named route with optional flash message.
protected
redirect(string $routeName[, array<string, mixed> $parameters = [] ][, array{0: string, 1: string}|null $flash = null ]) : never
Parameters
- $routeName : string
-
The name of the route to redirect to
- $parameters : array<string, mixed> = []
-
Route parameters
- $flash : array{0: string, 1: string}|null = null
-
Optional flash message as [$type, $message]
Return values
neverredirectBack()
Redirect back to the previous page or a fallback URL.
protected
redirectBack([string $fallback = '/' ][, array{0: string, 1: string}|null $flash = null ]) : never
Parameters
- $fallback : string = '/'
-
Fallback URL if referer is not available
- $flash : array{0: string, 1: string}|null = null
-
Optional flash message as [$type, $message]
Return values
neverredirectToUrl()
Redirect to a URL path with optional flash message.
protected
redirectToUrl(string $url[, array{0: string, 1: string}|null $flash = null ]) : never
Parameters
- $url : string
-
The URL path to redirect to
- $flash : array{0: string, 1: string}|null = null
-
Optional flash message as [$type, $message]
Return values
neverregisterAdd()
protected
static registerAdd(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerCreate()
protected
static registerCreate(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerDelete()
protected
static registerDelete(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerEdit()
protected
static registerEdit(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerIndex()
protected
static registerIndex(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerShow()
protected
static registerShow(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
registerUpdate()
protected
static registerUpdate(Application $app, string $controller, string $route) : void
Parameters
- $app : Application
- $controller : string
- $route : string
Tags
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
validationError()
Handle validation errors by redirecting with flash message.
protected
validationError(string $route, array<string, array<int, string>> $errors[, array<string, mixed> $routeParams = [] ]) : never
Parameters
- $route : string
-
Route name to redirect to
- $errors : array<string, array<int, string>>
-
Validation errors from DTO
- $routeParams : array<string, mixed> = []
-
Optional route parameters
Return values
neverview()
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
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