Application
Application
The Application.
The main application class is the core of the whole application and is responsible for routing actions to controllers based on request objects extended extended from Hazaar\Application\Request.
Supported request objects are:
- Hazaar\Application\Request\Http - A standard HTTP request. This can be either GET or POST requests. 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.
- Hazaar\Application\Request\Cli - This is a special request type to allow Hazaar applications to be executed from the command line. This is currently used by the config tool
Example usage:
define('APPLICATION_ENV', 'development');
$config = 'application.ini';
$application = new Hazaar\Application(APPLICATION_ENV, $config);
$application->bootstrap()->run();
Constants
VERSION
const VERSION = 3.0
Properties
GLOBALS
The global variables container.
public array $GLOBALS
This is a container for global variables that are available to all controllers and views. This is a way to pass data to all controllers and views without having to pass it through the controller chain.
The following variables are available by default:
- hazaar - Contains the HazaarMVC version and the time the application was started.
- env - The current application environment.
- path - The path to the application root.
- base - The base URL of the application.
- name - The name of the application.
request
public Request $request
config
public Config $config
loader
public Loader $loader
router
public Router $router
environment
public string $environment
timer
public Timer $timer
urlDefaultPart
protected string $urlDefaultPart
instance
public Application $instance
root
private string $root
outputFunctions
private array $outputFunctions
Methods
__construct
The main application constructor.
public __construct({Array})
The application is basically the center of the Hazaar MVC 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 piece of code executed within the HazaarMVC environment.
Because of this is it responsible for setting up the class loader, starting code execution profiling timers, loading the application configuration and setting up the request object.
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' |
shutdown
The main application destructor.
public shutdown({Array})
The 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({Array})
getDefaultConfig
public getDefaultConfig({Array})
configure
public configure({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$config | Config |
getInstance
public getInstance({Array})
setRoot
public setRoot({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$value | string |
getRoot
public getRoot({Array})
getRuntimePath
Returns the application runtime directory.
public getRuntimePath({Array})
The runtime directory is a place where HazaarMVC will keep files that it needs to create during normal operation. For example, socket files for background scheduler communication, cached views, and backend applications.
Parameters
Parameter | Type | Description |
---|---|---|
$suffix | NULL | An optional suffix to tack on the end of the path |
$createDir | boolean | automatically create the runtime directory if it does not exist |
getFilePath
public getFilePath({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$path | string | |
$forceRealpath | bool |
getRequestedController
Get the currently requested controller name.
public getRequestedController({Array})
getApplicationPath
Get the real path to the application on the local filesystem resolving links.
public getApplicationPath({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$suffix | NULL | application path suffix |
getBasePath
Get the base path.
public getBasePath({Array})
The 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({Array})
Bootstrap is the first step in running an application. It will run some checks to make sure sure the server has any required modules loaded as requested by the application (via the config). 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({Array})
Once the application has been initialised and a controller loaded, it can be executed via the run() method. This will execute the loaded controller and check that it returns a valid [[Hazaar\Controller\Response]] object. ifa 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. ifan 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
public getPath({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$path | string |
getURL
Generate a URL relative to the application.
public getURL({Array})
This 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
isActive
Test if a URL is active, relative to the application base URL.
public isActive({Array})
Parameters 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');
getVersion
Return the current Hazaar MVC framework version.
public getVersion({Array})
getResponseType
Returns the requested response type.
public getResponseType({Array})
The requested response type can be set in the request itself. If it is not set, then the default will be 'html' or the X-Requested-With header will be checked to determine the response type.
This method is used internally to determine the response type to send when one has not been explicitly used. Normally the response type is determined by the Controller\Response object type returned by a controller action.
setResponseType
Set the response type override for a request.
public setResponseType({Array})
The 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 |
registerOutputFunction
Register an output function.
public registerOutputFunction({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$function | callable |
Generated by Hazaar API Doc Generator