Web Gear is a free, lightweight open source web development platform written in PHP, aimed at developing fast web applications, based on a well defined MVC structure.

Controllers

Overview

The controller handles a user's action and performs all the low-level operations. Thus, only one instance of a controller is generally present. You can instantiate another controller from within an existing controller, but that is rarely required and implies a set of actions that must be taken by the programmer in order to control the flow of the application.

The controller only processes user input and does action specific operations. It does NOT produce any output all by itself, although it might look just like that at a first glance.

The first time a controller is started, it runs its own internal procedures (like creating some shortcuts of the libraries inside of it, for easier manipulation). After doing that (which is completely transparent to both the programmer and the user), the control flow is passed on to the programmer, whom is free to do whatever he/she likes.

All controllers must reside into the includes/controllers/ directory.

There is only one thing you might want to know before actually diving into the controller's world, and that is: an action represents a subset of instructions executed on a single request (given the fact that the HTTP protocol is stateless). An action is simply defined as a class method, but can become very powerful as the application grows. Don't think of an action as a boundary. You will see how much you can do from within an action as we explain the common methods and show you some examples.

Common methods

string Controller->getName()

Returns a string representing the controller's name (the class name, without the _Controller suffix).

string Controller->getAction()

Returns a string representing the controller action that is being executed (without the Action suffix).

mixed Controller->render([string mode = 'echo'])

This method is called after the action finished its execution, prior to the call to the __destruct() method of the controller.

Parameters

  • mode: the rendering mode. If the output from this controller is required later in the script, set this to return. The default mode is to echo the output, instead of returning it

Return values

  • string: if the mode is set to return, this method will return the generated output
  • void: if the mode is set to echo, this method will print the generated output to the screen, and nothing will be returned