Controller
Controller
Base Controller class.
All controller classes extend this class. Normally this class would only be extended by the controller classes provided by Hazaar, as how a controller actually behaves and the functionality it provides is actually defined by the controller itself. This controller does nothing, but will still initialise and run, but will output nothing.
Properties
request
public Request $requestname
protected string $name = 'controller'statusCode
protected int $statusCodebasePath
protected string $basePathstream
protected bool $streamresponseType
protected ResponseType $responseType = 'ResponseType'helpers
protected array $helpersmiddlewareDispatcher
private Dispatcher $middlewareDispatchercachedActions
private array $cachedActionscachedResponses
private array $cachedResponsesresponseCache
public Adapter $responseCacheMethods
__construct
Controller initialisation method.
public __construct(Request $request): voidThis should be called by all extending controllers and is simply responsible for storing the calling request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Request | the application request object |
__toString
Convert the controller object into a string.
public __toString(): string__get
Get the specified helper object.
public __get(string $helper): ?HelperParameters
| Parameter | Type | Description |
|---|---|---|
$helper | string | the name of the helper to retrieve |
initialize
Initialize the controller.
public initialize(mixed $args): ?ResponseCalls the init() method if it exists. This allows subclasses to provide custom initialization logic by defining an init() method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$args | mixed |
run
public run(?Route $route): ResponseParameters
| Parameter | Type | Description |
|---|---|---|
$route | Route |
shutdown
public shutdown(Response $response): voidParameters
| Parameter | Type | Description |
|---|---|---|
$response | Response |
getName
Retrieves the name associated with this instance.
public getName(): stringsetStatus
Sets the HTTP status code for the response.
public setStatus(?int $code): voidParameters
| Parameter | Type | Description |
|---|---|---|
$code | int | The HTTP status code to set. If null, the status code will be unset. |
getStatus
Retrieves the current HTTP status code.
public getStatus(): intgetBasePath
Retrieves the base path for the current controller.
public getBasePath(): stringsetBasePath
Sets the base path for the controller.
public setBasePath(string $path): voidParameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the base path to set |
isActive
Test if a URL is active, relative to the application base URL.
public isActive(): boolParameters are simply a list of URL 'parts' that will be combined to test against the current URL to see if it is active. Essentially the argument list is the same as Hazaar\Application::url() except that parameter arrays are not supported.
Unlike Hazaar\Controller::active() this method tests if the path is active relative to the application base path. If you want to test if a particular controller is active, then it has to be the first argument.
- Example
$application->active('mycontroller');addHelper
Adds a helper instance to the controller.
public addHelper(string $name, mixed $args): ?HelperSearches for the helper class in predefined namespaces, allowing application-specific helpers to override built-in ones. Instantiates the helper with the provided arguments and stores it for later use.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the helper to add |
$args | mixed |
getHelper
Retrieves a helper instance by name.
protected getHelper(string $name): ?HelperConverts the provided helper name to lowercase and checks if it exists in the helpers array. If found, returns the existing helper instance. Otherwise, attempts to add and return a new helper instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the helper to retrieve |
cacheAction
Enable caching for a specific controller action.
protected cacheAction(string $actionName, int $timeout = 60, bool $private): boolThis method marks an action as cacheable and sets the cache timeout and privacy options. When the action is executed, its response may be cached and reused for subsequent requests.
Parameters
| Parameter | Type | Description |
|---|---|---|
$actionName | string | the name of the action to cache |
$timeout | int | the cache timeout in seconds (default: 60) |
$private | bool | whether the cache should be private (default: false) |
middleware
Creates and returns a new Middleware instance with the specified name.
protected middleware(string $name, mixed $args): HandlerParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the middleware to instantiate |
$args | mixed |
runAction
Run an action method on a controller.
protected runAction(string $actionName, array $actionArgs, bool $namedActionArgs): ResponseThis is the main controller action decision code and is where the controller will decide what to actually execute and whether to cache the response on not.
Parameters
| Parameter | Type | Description |
|---|---|---|
$actionName | string | |
$actionArgs | array | The arguments to pass to the action |
$namedActionArgs | bool |
stream
protected stream(string $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
$value | string |
cacheResponse
Cache a response to the current action invocation.
private cacheResponse(Route $route, Response $response): boolThis method stores the response for the given route in the cache if caching is enabled for the action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$route | Route | the route for which the response is being cached |
$response | Response | the response object to cache |
getCachedResponse
Retrieve a cached response for the given route, if available.
private getCachedResponse(Route $route): ?ResponseThis method attempts to fetch a cached response from the response cache using the cache key generated from the controller name, action, and arguments. Returns the cached Response object if found, or null otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
$route | Route | the route for which to retrieve the cached response |
getCacheKey
Get the cache key for the current action.
private getCacheKey(string $controller, string $action, ?array $actionArgs, ?string $cacheName): stringParameters
| Parameter | Type | Description |
|---|---|---|
$controller | string | the controller name |
$action | string | the action name |
$actionArgs | array | the action arguments |
$cacheName | string | the cache name |
Generated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:25 +0000