Controllers

Nova 1 Developer Resource

Controllers are classes that can be reached through the URL and take care of handling the request from the browser. A controller calls models and other classes to fetch the information and finally will pass everything to a view for output. If a URL like www.yoursite.com/example/index is requested, the first segment will be which controller is called ("example") and the second which method of that controller is called ("index").

Creating a controller

In Nova, controllers are put in the application/controllers directory. At the very least, they need to extend the Controller class. Below is an example of the controller "example":

class Example extends Controller {

	function Example()
	{
		parent::Controller();
	}

	function index()
	{
		$data['header'] = 'My New Controller';
		
		$this->template->write('title', $data['header']);
		$this->template->write_view('content', '_base_override/main/pages/example_index', $data);
		
		$this->template->render();
	}
}

Anyone familiar with PHP will recognize that our constructor doesn't use PHP's contruct() method nor do controller methods have a visibility scope (public, private, protected). This is because we maintain PHP 4 compatability. A future version of Nova 1 will require PHP 5.0 or higher, but for now, we recommend that if you're distributing your work to other Nova sims that you make sure all your code works on PHP 4.3.2 and higher.

Using more parameters from the URL

In addition to pulling the controller and method from the URL, Nova can also pull additional parameters from the URL that you can pass from page to page. It's important though that you realize the security implications of doing so. Anything like usernames, passwords or any sensitive information should not be passed over the URL.

For the URL www.yoursite.com/example/index/action/2, you can access the final two segments using the following code:

$data['type'] = $this->uri->segment(3);
$data['id'] = $this->uri->segment(4);

Notice that we're referencing segments 3 and 4 since segments 1 and 2 are the controller and method respectively. Once you have these segments, you can do any type of logic with them that you'd like using standard PHP logic.

Editing a controller

In Nova, controllers are a two part entity. The first part is the base controller, always located in application/controllers/base and having a "_base" after the name of the file. This is the file we create and shouldn't be edited. Instead, if you want to edit a controller, you should use the file located in application/controllers. So if you wanted to edit the "sim" controller to edit an existing page, you would copy the method you want from application/controllers/base/sim_base.php and paste it into application/controllers/sim.php, make your edits, save it and upload it to the server.

Log in with your AnodyneID to rate this article or submit the article for review.