Fandom

Wikia Developers Wiki

Nirvana/WikiaController

< Nirvana

847pages on
this wiki
Add New Page
Talk0 Share

The main place for putting business logic is the Controller class. Typically a controller will just wrap queries and data related biz logic into useful functions for the front end. A function called getUserList(…) is a typical level of abstraction. The controller just deals with processing request variables and returning data in the response. The presentation as html/data/json is chosen by the caller. Each controller should use memcache directly to cache queries, we do not have a generic higher level cache in the application.

Places to NOT use a controller

  • I would generally recommend not using Controllers for Hooks. Put hook handling functions in a separate class, which may dispatch to a Controller. The reason for this is that the Hook invokes methods directly without going through the dispatcher so none of the $app, $wf or $wg or Request variables are set. We are considering adding this feature to the WikiaHookDispatcher.

Controller class features

  • The controller class must extend WikiaController AND have Controller in the name.
  • The controller class init() function is called before any (and all) actions are dispatched. Avoid putting heavyweight initialization specific to one method here, but data which is needed by all actions should go here.
  • Each controller has one or more "actions" which the Dispatcher can call. Any public function counts. You can have private helper methods in a controller but also consider making a helper class or Service class for utility functions.
  • In order to invoke a Controller method and just get data back, use the WikiaApp->sendRequest() method. A controller can also send a request to itself with the shorthand $this->sendSelfRequest(). For example:
// Send request to a different controller
$data = $app->sendRequest('Example', 'getData');
 
// Call a different method on the same controller
$data = $this->sendSelfRequest('doTask', array('paramKey' => 'paramValue'));
  • In order to invoke a Controller from inside a template, or if you want the HTML for some other reason, you can get that in two ways:
echo $app->renderView("controller", "method");
 
// or the slightly more verbose form 
 
$html = F::app()->renderView( 'foo', 'bar', array('param' => $val) );
  • Finally, if you are in a javascript context, you can get HTML from any controller using the generic ajax entry point:

http://foo.wikia-dev.com/wikia.php?controller=Foo&method=Bar&format=html

For security reasons, some classes extend WikiaService instead, which is identical to a controller to every aspect except that it is not accessible via the wikia.php entry point, and is only dispatchable via internal requests.

Template features

  • Controller data is contained in a WikiaResponse object which is made available to the template/View layer. Since this is a main feature of the class we have made it easier with some shorthand functions. Response variables can be set three ways:
// These are all equivalent
 
$this->response->setVal("variable", $value);  // Extra verbose
 
$this->setVal("variable", $value);  // Medium verbose
 
$this->variable = $value;  // Preferred form
  • A special note about the preferred form. If a member variable of the Controller exists, it must not have the same name as a template variable. Setting a variable will obey the proper scoping rules here, so if a local class variable exists, it will be set, and the variable will NOT be exported into the template. An example in code is given below. For the veterans, note that this is the opposite behavior from the old Oasis Module class, which required the public var to be declared.
  • A special note about globals. We DO NOT need to export global variables to the template any more. Oasis had a shortcut form for this, but now that we have the $wg shortcut everywhere it makes more sense for templates to use this.
  • Any public method of a controller can be called via Ajax. This is useful for debugging, especially when called with "raw" output mode via wikia.php:

http://name.wikia-dev.com/wikia.php?controller=Example&method=getData&format=raw

  • To render the output of a controller as HTML (in a template) call
 $app->renderView("controllerName", "controllerMethod");

Controller class example code

class ExampleController extends WikiaController {
 
  var $myVar;  // declaring a variable here will "mask" the template var
 
  public function init() {
    $this->myVar = "foo";   // will be set but not exported
  }
 
  public function getData() {
    $this->data = "bar";    // available in template
 
    $this->globalVar = $this->wg->globalVar;  // NOT NECESSARY!
  }  
 
  public function doTask() {
    $paramValue = $this->request->getVal('paramName');  // Verbose
 
    $paramValue = $this->getVal('paramName');  // equivalent shorthand
  } 
 
  // cannot be called via $app->sendRequest();
  private function doSomething() {
 
  }
 
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.