Application:Gateway

From Surebert wiki

Contents

Overview

The "gateway" between the web directory root and the private application directories and views. The Gateway class and all the classes required by the gateway (sb_Controller, sb_Request) are stored in /public/gateway.php

Generally, this should simple be an include statement, including the Gateway you are using in the project, e.g. include the trunk version. For production I would suggest using a stable tag instead

php code

require_once('/var/www/sbf/sb/trunk/Application/GatewayController.php');

The Gateway Class

Inside gateway.php is a static, non-instantiable Gateway class that acts processes the requests and maps renders the corresponding views found in /private/views

The Gateway class also has several static methods which can be called from within your views or models.

Static Properties

Gateway::$remote_addr

The remote address of the client. If the request is from the command line the agent is set to 127.0.0.1 otherwise it is set to the $_SERVER['REMOTE_ADDR']. If you are behind a proxy (e.g. the netscaler) and want to use another $_SERVER variable for the client IP, e.g. X-FORWARDED-FOR, you can create a custom App::set_remote_addr() method in App.php and the Gatway::$remote_addr will be populated from what that returns . Aliased in a views $this->request->remote_addr;

php code

class App{
 
	public static function set_remote_addr(){
		$proxy_addr = '12.3.4.5';
		if($_SERVER['REMOTE_ADDR'] == $proxy_addr && $_SERVER['X-FORWARDED-FOR']){
			return $_SERVER['X-FORWARDED-FOR'];
		} else if($_SERVER['REMOTE_ADDR']){
			return $_SERVER['REMOTE_ADDR'];
		} else {
			Gateway::$remote_addr;
		}
	}
}

Gateway::$agent

The user agent of the client. If the request comes from the command line then the agent is "command line" Aliased in a views $this->request->agent;

Gateway::$controller

A reference to the main controller handling the request

Gateway::$request->post

The $_POST data after being passed through an filters that you created. $_POST itself is removed to encourage programmers to use the filters. If the method App::filter_all_input exists then Gateway::$post is filtered through it first. Aliased as Gateway::$post.

Gateway::$request->data

The incoming data from POST, PUT, or DELETE requests as key value pairs

Gateway::$request->cookie

The $_COOKIE data from the client request. Aliased as Gateway::$cookie.

Gateway::$request->files

The $_FILES data from the client request. Aliased as Gateway::$files. Regular $_FILES is unset by the gateway.

e.g. Gateway::$files['YOUR_INPUT_NAME'] = $_FILES['YOUR_INPUT_NAME'];

php code

$new_location = ROOT.'/private/cache/myfile.ext';
if(!move_uploaded_file(Gateway::$files['YOUR_INPUT_NAME']['tmp_name'], $new_location)){
	throw(new Exception('The file could not be moved to its final destination at '.$new_location));
}

Static Methods

Gateway::render_request()

You can include one view in other with an additional call to the Gateway If you want to load is another template in another view domain or you want to pass arguments use the Gateway to load it with:

php code

echo Gateway::render_request('/view/template/arg');
//e.g.
echo Gateway::render_request('/user/sit/up');

With simple GET data if desired

php code

echo Gateway::render_request('/user/sit?one=two');

With other more complex data, use a new sb_Request() instance. You can set any other properties of the sb_Request this way

php code

$request = new sb_Request('/user/sit');
$request->post = Array('cat' => 'cheshire');
$request->get = Array('fish' => 'trout');
echo Gateway::render_request($request);

Any view included in this manor has the $this>included property set to true. The main views $this->included property is false.

Gateway::file_read()

If you want to read the contents of any file into a variable use the following syntax. If the file is not found it will return false.

php code

$var = Gateway::file_read('/public/css/app.css');

Gateway::file_require()

If you want to simply require a non .view template file use the following syntax. If the file is not found, it throws a fatal error.

php code

Gateway::file_require('/public/css/app.css');