Navigation

Overview

Controllers are your entry point for user interaction. The methods (actions) here will control the pages users see and the data they are shown

Within controllers you can change, set or disable the current view, as well as assigning variables to the view.
You can instantiate models here to retrieve information from the database and get information about the current request.


Before Load

The beforeLoad method is a built-in function that is executed before controller actions or view rendering.
This method should be declared with visibility level of protected.


beforeLoad Example

  protected function beforeLoad() {
    if(!$this->user) {
      // do stuff
    } else {
      // do other stuff
    }
  }
    

If you wish to have the same beforeLoad on each controller, it may be best to have a new base which extends the Controller class.
Note the use of an abstract class which can only be extended.
Also, our new base controller should be placed outside of the Core folder, preferably in the controllers directory.


Extending the controller class

  // BaseController.php
  abstract class BaseController extends Controller{  
      protected function beforeLoad() {
          if(!$this->user){
            // do stuff
          }
      } 
  }

  // IndexController.php
  class IndexController extends BaseController{
    
    // beforeLoad stuff happens

    public function indexAction(){
      // different stuff happens
    }
  }
    


Actions

The true entry points within controllers are the public methods suffixed with 'Action'.
(Please see configuration defaults for changing action suffix.)



    public function indexAction(){
      $myVar = "action";
      echo "Ready for " . $myVar;
    }
    


View


About Views

We use Smarty for views - documentation can be found here.
View template files have a .tpl file extension.

In element, views can be manually set, but will be automatically set if the following is true.

  • There is a folder in views named after the controller you are in.
  • There is a .tpl file with the name of the action in that folder. e.g: index.tpl.


Walkthrough:

  • The controller here is documentationController.
  • The action here is controllers.
  • We have a subdirectory in views named documentation.
  • documentation directory contains a file named controllers.tpl.
  • The view is automatically set.

│   
└──views
    │
    └──documentation
        controllers.tpl
    

If you navigated here via the links underneath Methods in the side menu, this list might be a little contradictory as you will see the action for that is controllers_methods ...
That is because we manually set the view for that action in order to share the template file.
See Setting Views further down for more information on that last part.


View Variables


Pass variables to a view using 'assign'

    public function indexAction(){
      $myVariable = "nice";
      $this->view->assign('viewVar', $myVariable); // name | value
    }
    

Use variable in view

    <p>
      Have a {$viewVar} day!
    </p>
    

Default Variables

There is currently only one default variable: absoluteUrl

The reason for this is to provide an easy prefix when declaring a url. Relative urls don't always work as expected here!

Because this is a variable used in this page, writing it will output our domain + root, so we will call it 'absolute' for this example.


    <a href="{$absolute}somecontroller">Go!</a>
    

Disabling views

Disabling the view is very simple.


    public function indexAction(){
      $this->view->disable();
    }
    

Setting Views

At times you may wish to set or override the view. To do this, simply invoke the setView and showView methods.

If you navigated here via anything other than 'Overview', you will see that the action is actually controllers_methods because we manually set the view.


    public function controllers_methodsAction() {
     
      // set the view
      $this->setView('controllers');

      // set some variables
      $this->view->assign('stuff', 'oh yeah');

      // show the view
      $this->showView();
      
    }
    

Note that we don't need to include the subdirectory name (element) or the .tpl extension


Request

Overview

The Request class, as suggested by the name, contains information about the request

From a controller, the request object can be accessed using $this->request->[method].

Methods

Check request type using the lowercase names of the request types

Reference Options Returns
$this->request->
get true/false
put
post
delete
head

By accessing the request object, we can also access POST and GET values, via arrays containing those values

  • $this->request->getVars
  • $this->request->postVars