Application:Controllers

From Surebert wiki

Contents

Framework:Controllers Overview

The controllers basically intercept all requests coming in from the Gateway and respond back with the appropriate data.

The controller being used to respond to the request is also the $this of any .view file that is rendered. The behind the scenes controller is called Gateway. It passes off control to the default Controller which is called IndexController and is found in the /private/controllers directory.

By default IndexController extends sb_Controller_HTML5 which in turn extends sb_Contoller

sb_Controller Properties

$request

The $this->request property of a sb_Controller is an instance of sb_Request modeling the request that was given to the controller. You can access this from any method of a controller other than __construct() at which time you can only refer to Gateway::$request.

sb_Controller Methods

__construct

Called when the controller is instantiated. The $this->request is not available at this point. See on_before_render() below if you need that.

on_before_render()

Similar to __construct(), on_before_render() is called before any methods are called to render a response. Unlike the constructor you do have access to the $this->request property at this point. You can use on_before_render() to do things like check for GET query variables, cookies, admin status, etc. If you ever return false from on_before_render, all executation is stopped at that point. This is a great method to use for common security checks that would need to be called for each servable method.

render_view('/admin/login');

This loads .view template e.g. /views/admin/login.view and returns the resulting contents. The $this of the .view template is the Controller that called $this->render_view('/admin/login); For rendering a totally different request or overriding arguments of the Gateway::$request see Framework:Gateway#Gateway::render_request.28.29

not_found()

This is fired when no there is no matching method or .view to handle the request. The default is a 404 error page. You can always redefine this. You have access to the full request in this method if you want to do anything custom based on the request.c

__destruct

Called when the Controller is destructed. You could use this to disconnect from resources, log time/memory, etc

render()

Renders the data based on the type of controller. You do not usually call the method, however, you can redefine it to create different types of controllers. The default action is to look for a method name that matches the request and if it is marked as @servable to return the result to the output buffer. This method does not need to be called manually as it is called by the Gateway. You could always redefine the render method for custom controllers. See Framework:sb_Controller_REST, Framework:sb_Controller_JSON_RPC2 as examples of controllers who's render method changes the behavior of the controller.

Example

The default render method of sb_Controller works this way. It looks for a method that matches the request and is marked as @servable e.g. /car/drive would call the CarControllers drive method. See example below.

php code

<?php
class CarController extends sb_Controller{
 
	/**
	 * Test method to demonstrate servable methods
	 * @return string
	 * @servable true
	 */
	public function drive(){
		return 'is driving';
	}
 
	/**
	 * fires when no servable method is found
	 */
	public function not_found(){
		//do something, then throw 404
		parent::not_found();
	}
}
?>

Input and Method Properties

Each public method has additional properties which can be defined in the phpdocs of the methods themselves. These properties include:

  • @servable (true | false) Determines if the method is accessible via URL. True by default
  • @input_as_array (true | false) Determines if arguments are passed in order or as a named hash like GET and POST
  • @http_method (get | post) Determines which type of data is passed to the method POST or GET

input_as_array

For the following two examples we are assuming the url is /car/paint?color=ACACAC&detail=1

A method that has input_as_array true would receive input like like

php code

/**
* @servable true
* @http_method get
* @input_as_array true
*/
public function some_method($input){
	//$input['color'] = 'ACACAC';
	//$input['detail'] = '1';
}

While a method that has @input_as_array set to false would receive input values in the order they came with the keys. In this case the order of the arguments is very important and should only be used when you can be sure of the order.

php code

/**
* @servable true
* @http_method get
* @input_as_array false
*/
public function some_method($color, $detail){
	//$color = 'ACACAC';
	//$detail = '1';
}