webCOMAND

io_comand_mvc\controller

A class for cMVC controllers to extend that provides convenience methods to access the COMAND repository, configurations, views, and to produce common file and error responses.

Controllers are not required to extend this controller class.  If a controller does not extend controller, it must access the repository, configurations, views and produce responses using other methods.

Controller Example

The following cMVC Controller would typically be located at a file path like:
packages/com_example_www/controllers/contacts.php

<?php
namespace com_example_www\controllers;

class contacts extends \io_comand_mvc\controller {

    // the default controller method, which will handle requests to:
    // https://www.example.com/contacts
    public function web__index(){
        $contacts = $this->repo()->get("FROM Contact");
        $this->view('contact_list', ['contacts'=>$contacts]);
    }

    // handle show requests to:
    // https://www.example.com/contacts/show/<OID>
    // https://www.example.com/contacts/show?oid=<OID>
    public function web__show($oid){
        $contact = $this->repo()->get_first('FROM Contact WHERE OID=?', [
            'bind'=>[$oid]
        ]);
        if($contact){
            $this->view('contact', ['contact'=>$contact]);
        } else {
            $this->show_error("Invalid contact specified.");
        }
    }
    // handle delete AJAX requests to:
    // https://www.example.com/contacts/delete/<OID>
    // https://www.example.com/contacts/delete?oid=<OID>
    public function web__delete(){
        $oid = $this->ajax->oid;
        $contact = $this->repo()->get_first('FROM Contact WHERE OID=?', [
            'bind'=>[$oid]
        ]);
        if($contact){
            $contact->delete();
        } else {
            $this->show_error("Invalid contact specified.");
        }
    }
}

Properties

The following properties are available from a controller as $this->property, where property is one of the following.

  • ajax - If the controller is responding to an AJAX request, the ajax property will be set to an instance of io_comand_web\ajax.
  • base_url - A string containing the base URL used to access this package.  It excludes the controller and any other part of the URL after the package name (if the package name is in the URL).
  • request - An instance of io_comand_web\request.
  • router - The instance of io_comand_mvc\router that was used to route this request.

Router Vars

In addition to the properties listed above, variables passed into router::route() through the vars option become available as properties of $this inside the controller.  For example the router var 'user' becomes $this->user.