Application
Application
The Application.
The main application class is the core of Hazaar and serves as the entry point to your application. It is responsible for routing actions to controllers based on an HTTP request. (CLI requests are handled by Hazaar\Console\Application.)
The Application will manage access to a Hazaar\Application\Request object that can be used to obtain information about the request. This request can be either a GET or POST request.
POST requests can optionally have a request body. In this case the Content-Type header will be checked to see how to decode the request body. multipart/form-data and application/json are currently accepted.
Example entry point usage:
$application = new Hazaar\Application();
$application->bootstrap()->run();Constants
VERSION
public const VERSION = '3.0'Properties
config
public Config $configloader
public Loader $loaderrouter
public Router $routerenvironment
public string $environment = 'development'path
public string $pathbase
public string $basetimer
public Timer $timerurlDefaultPart
protected string $urlDefaultPartinstance
public Application $instanceroot
private string $rootruntime
private Runtime $runtimeeventDispatcher
private EventDispatcher $eventDispatchermiddlewareDispatcher
private Dispatcher $middlewareDispatcherMethods
__construct
The main application constructor.
public __construct(string $env, ?string $path): voidThe application is basically the center of the Hazaar universe. Everything hangs off of it and controllers are executed within the context of the application. The main constructor prepares the application to begin processing and is the first bit of code executed within the Hazaar environment.
The constructor is responsible for setting up the class loader, starting code execution profiling timers, loading the application configuration and setting up the router.
Hazaar also has a 'run direct' function that allows Hazaar to execute special requests directly without going through the normal application execution path. These requests are used so hazaar can serve static internal content such as error pages, images and style sheets required by these error pages and redistributed JavaScript code libraries.
Parameters
| Parameter | Type | Description |
|---|---|---|
$env | string | The application environment name. eg: 'development' or 'production' |
$path | string |
shutdown
The main application destructor.
public shutdown(): voidThe destructor cleans up any application redirections. ifthe controller hasn't used it in this run then it loses it. This prevents stale redirect URIs from accidentally being used.
getConfigOverridePaths
public getConfigOverridePaths(): voidgetDefaultConfig
public getDefaultConfig(): voidconfigure
public configure(Config $config): voidParameters
| Parameter | Type | Description |
|---|---|---|
$config | Config |
getInstance
Get the current application instance.
public getInstance(): ?ApplicationThis static function can be used to get a reference to the current application instance from anywhere.
setRoot
public setRoot(string $value): voidParameters
| Parameter | Type | Description |
|---|---|---|
$value | string |
getRoot
public getRoot(): stringgetRuntimePath
Returns the application runtime directory.
public getRuntimePath(): stringThe runtime directory is a place where Hazaar will keep files that it needs to create during normal operation. For example, socket files for background scheduler communication, cached views, and backend applications.
getFilePath
Return the requested path in the current application.
public getFilePath(?string $path, bool $forceRealpath = true): stringThis method allows access to the raw URL path part, relative to the current application request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | path suffix to append to the application path |
$forceRealpath | bool | Return the real path to a file. If the file does not exist, this will return false. |
getRequestedController
Get the currently requested controller name.
public getRequestedController(): stringgetAppPath
Get the real path to the application on the local filesystem resolving links.
public getAppPath(NULL $suffix): stringParameters
| Parameter | Type | Description |
|---|---|---|
$suffix | NULL | application path suffix |
getBasePath
Get the base path.
public getBasePath(NULL $suffix): stringThe base path is the root your application which contains the application, library and public directories
Parameters
| Parameter | Type | Description |
|---|---|---|
$suffix | NULL | application base path suffix |
bootstrap
Initialise the application ready for execution.
public bootstrap(): ApplicationBootstrap is the first step in running an application. The process will set up error handling, register the configure locale and timezone, initialise the Hazaar\Application\Router and load any predefined routes (see: Routing).
Lastly it will then execute the application bootstrap.php script within the context of the application. Once that step succeeds the requested (or the default) controller will be loaded and initialised so that it is ready for execution by the application.
run
Executes the application.
public run(?Controller $controller): intOnce the application has been bootstrapped it is ready to run. A Hazaar\Controller object can be passed as an argument or if omited the Hazaar\Application\Router will be used to evaluate the Hazaar\Application\Request to determine the controller to load and execute.
The requested controller will be executed and the application will check that it returns a valid Hazaar\Controller\Response object. If a valid response is not returned an exception will be raised.
Once a valid response object is returned it will be used to write output back to the web server to be returned to the user.
This method also has protection against error loops. If an exception is thrown while processing a Hazaar\Controller\Error controller object then a new exception will be thrown outside the application context and will display basic fall-back error output.
Parameters
| Parameter | Type | Description |
|---|---|---|
$controller | Controller |
getPath
Return the requested path in the current application.
public getPath(?string $path): stringThis method allows access to the raw URL path part, relative to the current application request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string |
getURL
Generate a URL relative to the application.
public getURL(): URLThis is the base method for generating URLs in your application. URLs generated directly from here are relative to the application base path. For URLs that are relative to the current controller see Controller::url()
Parameters are dynamic and depend on what you are trying to generate.
For examples see: Generating URLs
getVersion
Return the current Hazaar framework version.
public getVersion(): stringsetResponseType
Set the response type override for a request.
public setResponseType(string $type): voidThe response type should be set in the response object itself. However, setting this allows that to be overridden. This should be used sparingly but can be used from a controller to force reponses to a certain type, such as application/json.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | string |
addMiddleware
Adds a middleware to the middleware dispatcher.
public addMiddleware(Middleware $middleware): voidParameters
| Parameter | Type | Description |
|---|---|---|
$middleware | Middleware | the middleware instance to add |
errorHandler
Custom error handler function.
public errorHandler(int $errno, string $errstr, ?string $errfile, ?int $errline): boolThis function is responsible for handling PHP errors and displaying appropriate error messages.
Parameters
| Parameter | Type | Description |
|---|---|---|
$errno | int | the error number |
$errstr | string | the error message |
$errfile | string | the file where the error occurred |
$errline | int | the line number where the error occurred |
shutdownHandler
Shutdown handler function.
public shutdownHandler(): voidThis function is responsible for executing the shutdown tasks registered in the global variable $__shutdownTasks. It checks if the script is running in CLI mode or if headers have already been sent before executing the tasks.
exceptionHandler
Exception handler function.
public exceptionHandler(\Throwable $e, ?int $responseType): voidThis function is responsible for handling exceptions thrown in the application. If the exception code is greater than or equal to 500, it logs the error message along with the code, line number, and file name. Then it calls the errorAndDie() function to handle the error further.
Parameters
| Parameter | Type | Description |
|---|---|---|
$e | \Throwable | |
$responseType | int |
getBase
public getBase(): stringfindAppPath
public findAppPath(?string $searchPath): ?stringParameters
| Parameter | Type | Description |
|---|---|---|
$searchPath | string |
Generated by Hazaar API Doc Generator on Mon, 27 Oct 2025 13:01:29 +0000