Controller
Controllers are placed inside the src/controller directory. As a convention all controller names have to end on Controller.php. Actions contained in there must end on Action. For instance we create a src/controller/indexController.php file with the following content:
<?php
namespace src\controller;
class indexController extends \hubert\generic\controller {
public function indexAction($params){
return $this->responseTemplate("index/index", ["name" => "Hubert"]);
}
public function apiAction(){
$data = ["users" => ["Falk", "Ronny"]];
return $this->responseJson($data);
}
public function redirectAction(){
$home_route = hubert()->router->get("home");
return $this->responseRedirect($home_route);
}
}
Controllers inherit from \hubert\generic\controller or you implement your own interface at hubert\interfaces\controller. The response object is avaliable using $this->getResponse() and the request object is available using $this->getRequest(). Controllers always have to return a response object. To populate it with some data there are three ways to do so:
- $this->responseJson($data, $status = null, $encodingOptions = 0) using this function an object for instance an array is set to the response and the response itself is encoded as json
- $this->responseRedirect($url, $status = null) a url is set to the response and delivered as redirect to he receiver
- $this->responseTemplate($template, $data = array()) renders a template