\Hazaar
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();

Summary
Methods Properties Constants
run
url
No constants
Properties
$bootstrap
$bootstrap
$config
$config
$environment
$environment
Static
$instance
$instance
$loader
$loader
$request
$request
$response
$response
$timer
$timer
Methods
__construct()
__construct($env)
The main application constructor

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.

Tags

Since

1.0.0

Parameters

$envstring

The application environment name. eg: ‘development’ or ‘production’

__destruct()
__destruct()
The main application destructor

The destructor cleans up any application redirections. ifthe controller hasn’t used it in this run then it loses it. This prevents stale redirect URLs from accidentally being used.

Tags

Since

1.0.0

bootstrap()
bootstrap($simple_mode = false) : \Hazaar\Application
Initialise the application ready for execution

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.

Tags

Since

1.0.0

Parameters

$simple_mode

No description

Returns

\Hazaar\Application

Returns a reference to itself to allow chaining

Static
filePath()
filePath($path = null, $file = null, $force_realpath = true)
Return the requested path in the current application

This method allows access to the raw URL path part, relative to the current application request.

Tags

Since

1.0.0

Parameters

$path

No description

$file

No description

$force_realpath

No description

getApplicationPath()
getApplicationPath($suffix = '') : string
Get the real path to the application on the local filesystem resolving links

Tags

Since

1.0.0

Parameters

$suffix

No description

Returns

string

The resolved application path

getBasePath()
getBasePath($suffix = '') : string
Get the base path

The base path is the root your application which contains the application, library and public directories

Tags

Since

1.0.0

Parameters

$suffix

No description

Returns

string

The resolved base path

Static
getInstance()
getInstance() : \Hazaar\Application
Get the current application instance

This static function can be used to get a reference to the current application instance from anywhere.

Tags

Since

1.0.0

Returns

\Hazaar\Application

The application instance

getRequestedController()
getRequestedController() : string
Get the currently requested controller name

Returns

string

The current controller name

Static
path()
path($path = null)
Return the requested path in the current application

This method allows access to the raw URL path part, relative to the current application request.

Tags

Since

1.0.0

Parameters

$path

No description

redirect()
redirect($location, $args = Array ( ) , $save_url = true)
Send an immediate redirect response to redirect the browser

It’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.

Tags

Since

1.0.0

Parameters

$location

No description

$args

No description

$save_url

No description

redirectBack()
redirectBack()
Redirect back to a URL saved during redirection

This mechanism is used with the $save_url parameter of Application::redirect() so save the current URL 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 redirectBack() method to redirect the user back to the page they were originally looking for.

Tags

Since

1.0.0

run()
run($controller = null)
Executes the application

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.

Tags

Since

1.0.0

@exception Application\Exception\InvalidResponse ifthe controller returns an invalid response object

@exception Application\Exception\ErrorLoop ifan error was encountered while processing a Hazaar\Controller\Error object

Parameters

$controller\Hazaar\Controller

No description

runStdin()
runStdin()
Execute code from standard input in the application context

This method is part of the background code execution scheduler, Bulletin. Code can be scheduled to execute in the background at a later time. This code is stored in the schedulars memory and when it’s time to execute it, Bulletin will launch a new application context, bootstrap it, but instead of calling the run() method like normal it will call this runStdin() method to execute the stored code that was passed via stdin.

Tags

Since

1.0.0

runtimePath()
runtimePath($suffix = null, $create_dir = false) : string
Returns the application runtime directory

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.

Tags

Since

1.0.0

Parameters

$suffix

No description

$create_dir

No description

Returns

string

The path to the runtime directory

Static
url()
url()
Generate a URL relative to the application

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

Tags

Since

1.0.0

version()
version()