\Nano4\ControllersAdvanced

Advanced controller.

Provides far more functionality than the Basic controller. Including:

  • Array access for view data.
  • Authenticated users using SimpleAuth.
  • Model configuration, including Database integration.
  • Translatable text and status messages.
  • A view loader for e-mail messages (now called 'mail')

You should define "page.default", "page.login" and "layout.default" options in your Nano object.

Summary

Methods
Properties
Constants
offsetExists()
offsetGet()
offsetSet()
offsetUnset()
get_models_of_type()
get_models_with_type()
add_json()
get_text()
msg()
message()
store_messages()
redirect_msg()
go_msg()
error()
show_error()
redirect_error()
redirect_warn()
go_error()
go_warn()
warning()
warn()
has_status()
has_errors()
add_status_json()
get_user()
find_resource()
use_resource()
add_js()
add_css()
__construct()
display()
send_html()
send_json()
send_xml()
model()
name()
has_upload()
get_upload()
redirect()
go()
get_uri()
url()
request_uri()
current_url()
download()
is_post()
class_id()
$models
No constants found
__construct_defaults_controller()
__construct_modelconf_controller()
populate_model_opts()
get_model_opts()
get_db_model_opts()
__construct_messages_controller()
__construct_userauth_controller()
__construct_mailer_controller()
__construct_uservalidation_controller()
needs()
get_prop()
set_prop()
addWrapper()
$model_opts
$text
$lang
$status_types
$user
$resources
$data
$screen
$layout
$to_json_method
$to_xml_method
$called_constructors
$exception_handler
$__classid
N/A
No private methods found
No private properties found
N/A

Properties

$models

$models : 

Type

$model_opts

$model_opts : 

Type

$text

$text : 

Type

$lang

$lang : 

Type

$status_types

$status_types : 

Type

$user

$user : 

Type

$resources

$resources : 

Resources represent external files, such as scripts, stylesheets, etc.

They are managed through a generic system that allows for easy future expansion.

Each group as a resource id, such as 'js', 'css', etc, and the following definitions:

name: The variable name for templates. path: An array of paths to look for resource files in. exts: An array of extensions to look for resource files. groups: An array of arrays, each being named groups. It is recommended to prefix groups with an identifier such as '#'. We include '#common' and '#webapp' as example groups, they depend upon the Nano.js library set. added: An empty array, will be populated by use_resource();

A group for one resource type MAY in fact depend on a resource of another type. For instance, you may have a Javascript file that depends on a CSS stylesheet being loaded. You can define a rule that will include it, by using a 'type:name' format, such as 'css:foobar'.

Type

$data

$data : 

Type

$screen

$screen : 

Type

$layout

$layout : 

Type

$to_json_method

$to_json_method : 

Type

$to_xml_method

$to_xml_method : 

Type

$called_constructors

$called_constructors : 

Type

$exception_handler

$exception_handler : 

Type

$__classid

$__classid : 

The constructor will be passed a '__classid' option.

Ensure this property is populated with its value.

Type

Methods

offsetExists()

offsetExists(  $offset) 

Parameters

$offset

offsetGet()

offsetGet(  $offset) 

Parameters

$offset

offsetSet()

offsetSet(  $offset,   $value) 

Parameters

$offset
$value

offsetUnset()

offsetUnset(  $offset) 

Parameters

$offset

get_models_of_type()

get_models_of_type(  $type) 

Return a list of models with a specific ".type" definition, along with the flat model options.

[ 'modelname' => $model_opts, // more here ]

Parameters

$type

get_models_with_type()

get_models_with_type(  $type,   $opts = array()) 

Return a list of models that are decended from a type, no matter how deep in the group hierarchy they are. Also returns the expanded model options (as returned by get_model_opts() with @types enabled.)

[ 'modelname' => $extended_model_opts, // more here ]

Parameters

$type
$opts

add_json()

add_json(  $name,   $data) 

An easy way to add JSON data to the templates.

In your layout, you should have a section that looks like:

<?php if (isset($json) && count($json) > 0) foreach ($json as $json_name => $json_data) echo $html->json($json_name, $json_data); ?>

The above assumes you're using the Messages trait, which should be loaded after this one, and includes the $html template helper. You could also choose to build the JSON fields manually, but the helper is simpler.

Parameters

$name
$data

get_text()

get_text() 

msg()

msg(  $name,   $opts = array()) 

Parameters

$name
$opts

message()

message(  $name,   $opts = array()) 

Parameters

$name
$opts

store_messages()

store_messages() 

Store any current messages in the session, so they can be retreived on a redirect.

redirect_msg()

redirect_msg(  $name,   $url = Null,   $opts = array()) 

Parameters

$name
$url
$opts

go_msg()

go_msg(  $msg,   $page,   $params = array(),   $gopts = array(),   $mopts = array()) 

Parameters

$msg
$page
$params
$gopts
$mopts

error()

error(  $name,   $opts = array()) 

Parameters

$name
$opts

show_error()

show_error(  $name,   $opts = array()) 

Parameters

$name
$opts

redirect_error()

redirect_error(  $name,   $url = Null,   $opts = array()) 

Parameters

$name
$url
$opts

redirect_warn()

redirect_warn(  $name,   $url = Null,   $opts = array()) 

Parameters

$name
$url
$opts

go_error()

go_error(  $msg,   $page,   $params = array(),   $gopts = array(),   $mopts = array()) 

Parameters

$msg
$page
$params
$gopts
$mopts

go_warn()

go_warn(  $msg,   $page,   $params = array(),   $gopts = array(),   $mopts = array()) 

Parameters

$msg
$page
$params
$gopts
$mopts

warning()

warning(  $name,   $opts = array()) 

Parameters

$name
$opts

warn()

warn(  $name,   $opts = array()) 

Parameters

$name
$opts

has_status()

has_status(  $type) 

Parameters

$type

has_errors()

has_errors() 

add_status_json()

add_status_json(  $messages) 

Add a 'status_messages' JSON element.

Pass it a list of messages to include in the JSON.

Parameters

$messages

get_user()

get_user() 

find_resource()

find_resource(String  $type, String  $name) 

Find a resource file, based on known paths and extensions.

Parameters

String $type

The resource type.

String $name

The resource name without path or extension.

use_resource()

use_resource(String  $type, String  $name) 

Add a resource file to an array of resources for use in view templates.

Parameters

String $type

The resource type.

String $name

The resource or group name.

add_js()

add_js(  $name) 

Add a Javascript file or group to our used resources.

Parameters

$name

add_css()

add_css(  $name) 

Add a CSS stylesheet file or group to our used resources.

Parameters

$name

__construct()

__construct(  $opts = array()) 

Provide a default __construct() method that can chain a bunch of constructors together.

The list of constructors that will be called, and in what order, is dependent on the existence of a class property called $constructors. If the property exists, and is an array, then it is a list of keys, which expect a method called _construct{$key}_controller() is defined in your class (likely via trait composing.)

If the property does not exist, then we will get a list of all methods matching _construct{word}_controller() and will call them all in whatever order they were defined in.

Parameters

$opts

display()

display(  $opts = array()) 

Display the contents of a screen, typically within a common layout.

We use the $data class member as the array of variables to pass to the template.

Parameters

$opts

send_html()

send_html(String  $screen, Array  $data = Null) 

Sometimes we don't want to display a primary screen with a layout, but instead a sub-screen, with no layout, and using specified data.

Parameters

String $screen

The name of the screen view to use.

Array $data

(Optional) Variables to send to the screen view.

The $data defines the variables that will be made available to the screen view template. If you do not specify a $data array, then the $this->data class member will be used instead.

send_json()

send_json(Mixed  $data, Mixed  $opts = array()) 

Sometimes we want to send JSON data instead of a template.

Parameters

Mixed $data

The data to send (see below)

Mixed $opts

Options, see below.

If the $data is a PHP Array, then it will be processed using the json_encode() function. In this case, there is one recognized option (which is only applicable with PHP 5.4 or higher)

'fancy' If set to True, we will use human readable formatting on the JSON string (aka Pretty Printing.)

If the $data is an Object, then it must have a method as per the $this->to_json_method (default: 'to_json'), which will be called, and passed the $opts as its first parameter.

If the $data is a string, it will be assumed to be a valid JSON string, and will be sent as is.

Anything else will fail and throw an Exception.

send_xml()

send_xml(Mixed  $data, Mixed  $opts = Null) 

Sometimes we want to send XML data instead of a template.

Parameters

Mixed $data

The data to send (see below)

Mixed $opts

Options, used in some cases (see below)

If $data is a string, we assume it's valid XML and pass it through.

If $data is a SimpleXML Element, DOMDocument or DOMElement, we use the native calls of those libraries to convert it to an XML string. One caveat: we assume that DOMElement objects have an ownerDocument, and if they don't, this method will fail.

If the $data is another kind of object, and has a method as per the $this->to_xml_method (default: 'to_xml') then it will be called with the $opts as its first parameter.

Anything else will throw an Exception.

model()

model(Mixed  $modelname = Null, Array  $modelopts = array(), Array  $loadopts = array()) 

Return the requested Model object.

Parameters

Mixed $modelname

If set, must be a string, see below.

Array $modelopts

Options to pass to model, see below.

Array $loadopts

Options specific to this, see below.

If the $model is not specified or is Null, then we assume the model has the same basename as the current controller.

The $modelopts will be added to the parameters used in the class loader (which will in turn be passed to the constructor of the Model class.)

If the specified $model has been loaded already by this controller, by default we will return the cached copy, ignoring any new options.

The two $loadopts options we support are:

'forceNew' If set to True, we will always create a new instance of the model, even if we've loaded it before. If caching is on, it will override the previously loaded instance.

'noCache' If set to true, we will not cache the model instance loaded by this call.

name()

name() 

Return our controller base name.

has_upload()

has_upload(String  $fieldname) 

Do we have an uploaded file?

Uses the Nano4\File::hasUpload() static method.

Parameters

String $fieldname

The upload field to look for.

get_upload()

get_upload(String  $fieldname) 

Get an uploaded file. It will return Null if the upload does not exist.

Uses the Nano4\File::getUpload() static method.

Parameters

String $fieldname

The upload field to get.

redirect()

redirect(String  $url, \Nano4\Controllers\Opts  $opts = array()) 

Redirect to a URL.

Parameters

String $url

The URL to redirect to.

\Nano4\Controllers\Opts $opts

Options to pass to Nano4\Plugins\URL::redirect().

go()

go(  $page,   $params = array(),   $opts = array()) 

Go to a route.

This will throw an Exception if the route does not exist.

Parameters

$page
$params
$opts

get_uri()

get_uri(  $page,   $params = array()) 

Get a route URI.

variables, and once again, can only be used with no parameters.

Parameters

$page
$params

url()

url(  $ssl = Null,   $port = Null) 

Return our site URL. See Nano4\Plugins\URL::site_url() for details.

Parameters

$ssl
$port

request_uri()

request_uri() 

Return our request URI. See Nano4\Plugins\URL::request_uri() for details.

current_url()

current_url() 

Return our current URL. See Nano4\Plugins\URL::current_url() for details.

download()

download(  $file,   $opts = array()) 

Send a file download to the client's browser.

Ends the current PHP process.

See Nano4\Plugins\URL::download() for details.

Parameters

$file
$opts

is_post()

is_post() 

Is the server using a POST request?

class_id()

class_id() 

__construct_defaults_controller()

__construct_defaults_controller(  $opts = array()) 

Parameters

$opts

__construct_modelconf_controller()

__construct_modelconf_controller(  $opts = array()) 

Parameters

$opts

populate_model_opts()

populate_model_opts(  $model,   $opts) 

Parameters

$model
$opts

get_model_opts()

get_model_opts(  $model, Array  $opts = array(), Array  $behavior = array()) 

Get model options from our $this->model_opts definitions.

Parameters

$model
Array $opts

Current/overridden options.

Array $behavior

See below

If the $behavior['defaults'] is True, and we cannot find a set of options for the specified model, then we will look for a set of options called '.default' and use that instead.

A special option called '.type' allows for nesting option defintions. If set in any level, an option group with the name of the '.type' will be looked up, and any options in its definition will be added (if they don't already exist.) Groups may have their own '.type' option, allowing for multiple levels of nesting.

If $behavior['types'] is True, we build a list of all nested groups.

The '.type' rule MUST NOT start with a dot. The group definition MUST start with a dot. The dot will be assumed on all groups.

So if a '.type' option is set to 'common', then a group called '.common' will be inherited from.

get_db_model_opts()

get_db_model_opts(  $model,   $opts) 

Parameters

$model
$opts

__construct_messages_controller()

__construct_messages_controller(  $opts = array()) 

Parameters

$opts

__construct_userauth_controller()

__construct_userauth_controller(  $opts = array()) 

Parameters

$opts

__construct_mailer_controller()

__construct_mailer_controller(  $opts = array()) 

Parameters

$opts

__construct_uservalidation_controller()

__construct_uservalidation_controller(  $opts = array()) 

Parameters

$opts

needs()

needs(  $constructor,   $opts = array(),   $fullname = False) 

Parameters

$constructor
$opts
$fullname

get_prop()

get_prop(  $property,   $default = Null) 

Parameters

$property
$default

set_prop()

set_prop(  $property,   $value) 

Parameters

$property
$value

addWrapper()

addWrapper(String  $method, String  $varname = Null, Bool  $closure = False) 

Add a wrapper to a controller method that you can call from a view template (as a Callable.)

Parameters

String $method

The method we want to wrap into a closure.

String $varname

(Optional) The name of the callable for the views.

Bool $closure

(Default False) If true, use a closure.

If $varname is not specified, it will be the same as the $method.

As an example, a call of $this->addWrapper('url'); will create a callable called $url, that when called, will make a call to $this->url() with the specified parameters.