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
name
protected string $name = 'controller'request
protected Request $requeststatusCode
protected int $statusCodebasePath
protected string $basePath_helpers
private array $_helpersredirectCookieName
private string $redirectCookieName = 'hazaar-redirect-token'Methods
__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 |
initialize
Controller initialisation method.
public initialize(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 |
run
public run(?Route $route): ResponseParameters
| Parameter | Type | Description |
|---|---|---|
$route | Route |
shutdown
public shutdown(): voidgetName
Get the name of the controller.
public getName(): stringsetStatus
Set the default return status code.
public setStatus(?int $code): voidParameters
| Parameter | Type | Description |
|---|---|---|
$code | int |
getStatus
Get the status code of the controller.
public getStatus(): intgetBasePath
Get the base path of the controller.
public getBasePath(): stringsetBasePath
Set the base path for the controller.
public setBasePath(string $path): voidParameters
| Parameter | Type | Description |
|---|---|---|
$path | string |
redirect
Generate a redirect response to redirect the browser.
public redirect(string $location, bool $saveURI): RedirectIt's quite common to redirect the user to an alternative URL. This may be to forward the request to another website, forward them to an authentication page or even just remove processed request parameters from the URL to neaten the URL up.
Parameters
| Parameter | Type | Description |
|---|---|---|
$location | string | The URI you want to redirect to |
$saveURI | bool | Optionally save the URI so we can redirect back. See: Hazaar\Application::redirectBack() |
redirectBack
Redirect back to a URI saved during redirection.
public redirectBack(?string $altURL): RedirectThis mechanism is used with the $saveURI parameter of Hazaar\Application::redirect() so save the current URI into the session so that once we're done processing the request somewhere else we can come back to where we were. This is useful for when a user requests a page but isn't authenticated, we can redirect them to a login page and then that page can call this Hazaar\Application::redirectBack() method to redirect the user back to the page they were originally looking for.
Parameters
| Parameter | Type | Description |
|---|---|---|
$altURL | string |
getURL
Generate a URL relative to the controller.
public getURL(): URLThis is the controller relative method for generating URLs in your application. URLs generated from here are relative to the controller. For URLs that are relative to the current application see Hazaar\Application::url().
Parameters are dynamic and depend on what you are trying to generate.
For examples see: Generating URLs
addHelper
Add a helper to the controller.
public addHelper(string $helper, array $args, ?string $alias): boolParameters
| Parameter | Type | Description |
|---|---|---|
$helper | string | The helper to add to the controller. This can be a helper object, a helper class name or an array of helpers. |
$args | array | an array of arguments to pass to the helper constructor |
$alias | string | The alias to use for the helper. If not provided, the helper name will be used. |
hasHelper
Checks if a helper exists.
public hasHelper(string $helper): boolParameters
| Parameter | Type | Description |
|---|---|---|
$helper | string | the name of the helper to check |
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');findHelper
Find a helper class by name.
private findHelper(string $name): ?stringThis method searches for view helper classes based on the given name. The search order is important because it allows apps to override built-in helpers.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the helper class to find |
Generated by Hazaar API Doc Generator on Tue, 30 Sep 2025 00:01:36 +0000