What is a controller?
Controllers are the heart of your application as they determine how requests from the browser are handled by Nova. A controller is simply a PHP class that is named in a way that can be associated with a URI. In fact, Nova’s URI can provide some insightful information about what controller and method are being used for the page being viewed:- When a controller’s name matches the first segment of a URI, it will be loaded.
- The second segment of the URI determines which method in the controller gets called.
- If your URI contains more than two segments they will be passed to your method as parameters.
/main/contact
In the above example, the main
controller will be loaded and the contact
method will be called.
/characters/bio/77
In this example, the characters
controller will be loaded and the bio
method will be called. Additionally, you’ll be able to add an argument to your controller method to access the 77
in the URI. (This is what allows Nova to have access to the necessary data to show a specific character bio without having to hard-code everything.)
You can read more about how CodeIgniter’s controllers work in their documentation.
Extending controllers
In order to provide as much flexibility as possible, Nova is split up into two distinct layers: the core and the application. Any work Anodyne does on Nova lives inside “the core”. Any work that you do on your game’s site is “the application”. This is done to ensure that any update to Nova doesn’t reset the changes you’ve made to your installation of Nova.Core controllers
The “core” layer of Nova is considered anything that lives inside thenova
directory. (As an aside, this is what allows for the simplicity of only needing to replace the nova
directory when updating to the latest version.)
When it comes to controllers, you’ll find that all of Nova’s core controllers are located in the nova/modules/core/controllers
directory. To avoid naming conflicts, all of Nova’s core controllers are prefixed with nova_
.
Application controllers
The “application” layer of Nova is considered anything that lives outside of thenova
directory.
When it comes to controllers, all of Nova’s application controllers are located in the application/controllers
directory. Nova comes with all of the needed controllers out of the box, but if you want to create new sections with new pages, you can add your own controllers here.
Customizations
When you open an application controller, you’ll see a file that looks something like this:application/controllers/Main.php
foo
will map to a page with the URI of /main/foo
). This also means is that you can override any existing method with one of your own by adding a method of the same name in your application controller.
When it comes to overriding a controller method, the recommended way of doing that is to copy the method from the core controller and paste it into the application controller. You then have a working copy of the page from which to modify whatever you want.
Understanding controllers
Now that you understand how to extend one of Nova’s controllers, let’s dig deeper into the various pieces involved in a Nova controller.$data
Any data that will be sent to the view is stored inside of an array in the controller method called $data
. This array stores things like language items, form fields and controls, raw information out of the database, and much more. The odds are that anything you want to do or change is stored in the $data
array.
If you’re trying to debug and see what data Nova is sending to the browser, before the
Template::render()
call, you can write die(var_dump($data));
to stop executing the code and see what’s in the $data
variable.$data
is sent to the view in its entirety, this also means that if you want to add additional data to a view, you can simply assign it to a key on the $data
array and you’ll have access to it in the view files using the key name as the variable.
$js_data
Nova takes a similar approach to passing data from PHP to Javascript as well, but with an aptly named $js_data
array. Everything mentioned above applies to Javascript data.
Passing data from PHP to Javascript can lead to some unexpected results if you’re not careful. You should reference how Nova handles passing data to Javascript before attempting to do so yourself.
Templates
Nova uses a simple template library to render the entire page to the user’s browser. Each individual piece of the template is called a region. As Nova is executing its code, it will assign data to specific regions. The library will take all of the regions and render them to the screen.Interested in learning more about the template library Nova uses? Dig in to the
nova/modules/core/libraries/Nova_template.php
file in your Nova installation to learn more.title
- The title of the page used in thehead
of the HTML page_redirect
javascript
- The Javascript for the pagenav_main
- The main navigation for the pagenav_sub
- The secondary navigation for the pageflash_message
- Flash messages for indicating success/failure after a create, edit, or delete actioncontent
- The content of the pageajax
- Secondary content for the page in the way of modal pop-ups
In most cases, the regions that most Nova pages deal with are
content
, javascript
, and title
.