34 @\set_exception_handler(array ($this,
'_errorHandler' ));
37 $this->server->cors();
39 $this->contentType=
"application/json";
40 $this->server->_setContentType($this->contentType);
43 parent::__construct();
47 if(isset($this->restCache[
"authorizations"])){
49 return $this->server->isValid();
56 throw new \Exception(
'HTTP/1.1 401 Unauthorized, you need an access token for this request',401);
64 $this->server->connect($this);
68 $thisClass=\get_class($this);
69 if(!isset($this->model))
71 if(!isset($this->model)){
72 $modelsNS=$this->config[
"mvcNS"][
"models"];
73 $this->model=$modelsNS.
"\\".$this->responseFormatter->getModel($thisClass);
80 $this->server->finalizeTokens();
90 echo $this->responseFormatter->formatException($e);
94 \http_response_code($value);
99 if($db[
"dbName"]!==
""){
100 DAO::connect($db[
"type"],$db[
"dbName"],@$db[
"serverName"],@$db[
"port"],@$db[
"user"],@$db[
"password"],@$db[
"options"],@$db[
"cache"]);
112 $values=\json_decode($values,
true);
123 echo $this->responseFormatter->get($datas);
131 return $this->
getOne($id,
true,
true);
141 public function get($condition=
"1=1",$loadManyToOne=
false,$loadOneToMany=
false,$useCache=
false){
143 $condition=\urldecode($condition);
147 $datas=
DAO::getAll($this->model,$condition,$loadManyToOne,$loadOneToMany,$useCache);
148 echo $this->responseFormatter->get($datas);
149 }
catch (\Exception $e){
151 echo $this->responseFormatter->formatException($e);
162 public function getOne($keyValues,$loadManyToOne=
false,$loadOneToMany=
false,$useCache=
false){
163 $keyValues=\urldecode($keyValues);
167 $data=
DAO::getOne($this->model, $keyValues,$loadManyToOne,$loadOneToMany,$useCache);
169 $_SESSION[
"_restInstance"]=$data;
170 echo $this->responseFormatter->getOne($data);
174 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
179 return $this->responseFormatter->format($arrayMessage);
188 if(isset($_SESSION[
"_restInstance"])){
191 echo $this->responseFormatter->get($datas);
193 throw new \Exception(
"You have to call getOne before calling getOneToMany.");
203 if(isset($_SESSION[
"_restInstance"])){
206 echo $this->responseFormatter->get($datas);
208 throw new \Exception(
"You have to call getOne before calling getManyToMany.");
220 if(isset($instance)){
224 echo $this->responseFormatter->format([
"status"=>
"updated",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
226 throw new \Exception(
"Unable to update the instance");
230 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
242 if(isset($instance)){
246 echo $this->responseFormatter->format([
"status"=>
"inserted",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
248 throw new \Exception(
"Unable to insert the instance");
252 echo $this->responseFormatter->format([
"message"=>
"Unable to create ".
$model.
" instance"]);
263 public function delete(...$keyValues){
265 if(isset($instance)){
268 echo $this->responseFormatter->format([
"status"=>
"deleted",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
270 throw new \Exception(
"Unable to delete the instance");
274 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
_setValuesToObject($instance, $values=null)
Updates $instance with $values To eventually be redefined in derived classes.
static getRestResource($controllerClass)
static getOne($className, $keyValues, $loadManyToOne=true, $loadOneToMany=false, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key...
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
update(... $keyValues)
Update an instance of $model selected by the primary key $keyValues Require members values in $_POST ...
static connect($dbType, $dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[], $cache=false)
Establishes the connection to the database using the past parameters.
getById($id)
Default route for requiring a single object ("{id}","methods"=>["get","options"]) ...
Base class for controllers.
static getManyToMany($instance, $member, $array=null, $useCache=NULL)
Assigns / loads the child records in the $member member of $instance.
add()
Insert a new instance of $model Require members values in $_POST array .
static getAll($className, $condition='', $loadManyToOne=true, $loadOneToMany=false, $useCache=NULL)
Returns an array of $className objects from the database.
static isJSON()
Returns true if request contentType is set to json.
static getRestCacheController($controllerClass)
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.
static getOneToMany($instance, $member, $useCache=NULL, $annot=null)
Assign / load the child records in the $member member of $instance.
connect()
Realise the connection to the server To override in derived classes to define your own authentication...
getOne($keyValues, $loadManyToOne=false, $loadOneToMany=false, $useCache=false)
Get the first object corresponding to the $keyValues.
getOneToMany($member, $useCache=false)
static remove($instance)
Deletes the object $instance from the database.
getManyToMany($member, $useCache=false)
static update($instance, $updateMany=false)
Updates an existing $instance in the database.
index()
Returns all objects for the resource $model ("cache"=>false)
static insert($instance, $insertMany=false)
Inserts a new instance $ instance into the database.