LFT
  • Namespace
  • Class
  • Tree

Namespaces

  • Docoflow
    • Contracts
    • Entity
    • Facades
    • Models
    • Traits
  • Docolight
    • Agno
      • Traits
    • Container
    • Http
      • Contracts
    • Rest
      • Handler
      • Http
    • Support
      • Debug
      • Traits
  • Docotory
    • Traits
  • None

Classes

  • Docoflow\Docoflow
  • Docoflow\Entity\Group
  • Docoflow\Entity\Step
  • Docoflow\Entity\Verificator
  • Docoflow\Facades\Action
  • Docoflow\Facades\Activity
  • Docoflow\Facades\Flo
  • Docoflow\Facades\StateActivity
  • Docoflow\Flo
  • Docoflow\Models\Workflow
  • Docoflow\Models\WorkflowAction
  • Docoflow\Models\WorkflowActivity
  • Docoflow\Models\WorkflowGroups
  • Docoflow\Models\WorkflowNotification
  • Docoflow\Models\WorkflowState
  • Docoflow\Models\WorkflowStateActivity
  • Docoflow\Models\WorkflowStep
  • Docoflow\Models\WorkflowVerificator
  • Docolight
  • Docolight\Agno\AgnoModule
  • Docolight\Container\Container
  • Docolight\Http\Headers
  • Docolight\Http\JsonResponse
  • Docolight\Http\MimeResponse
  • Docolight\Http\Response
  • Docolight\Http\ResponseFactory
  • Docolight\Rest\Handler\RestfulErrorHandler
  • Docolight\Rest\Http\RestFulController
  • Docolight\Support\ActiveRecordWrapper
  • Docolight\Support\Arr
  • Docolight\Support\Carbonate
  • Docolight\Support\ClassLoader
  • Docolight\Support\Collection
  • Docolight\Support\CollectionDataProvider
  • Docolight\Support\Debug\Dumper
  • Docolight\Support\Debug\HtmlDumper
  • Docolight\Support\Facade
  • Docolight\Support\Factory
  • Docolight\Support\Fluent
  • Docolight\Support\Html
  • Docolight\Support\IterablePager
  • Docolight\Support\Repository
  • Docolight\Support\Set
  • Docolight\Support\Str
  • Docotory\Factory

Interfaces

  • Docoflow\Contracts\DocoflowContract
  • Docoflow\Contracts\ValidationStatus
  • Docolight\Http\Contracts\Arrayable

Traits

  • Docoflow\Traits\BulkValidator
  • Docoflow\Traits\Entity
  • Docoflow\Traits\HasMutator
  • Docoflow\Traits\Validable
  • Docolight\Agno\Traits\HasAssetsUrl
  • Docolight\Agno\Traits\HasAutoload
  • Docolight\Support\Traits\Macroable
  • Docotory\Traits\HasFactories

Exceptions

  • Docolight\Container\BindingResolutionException
  • Docotory\ResolvingTypeException

Functions

  • array_add
  • array_build
  • array_collapse
  • array_divide
  • array_dot
  • array_except
  • array_first
  • array_flatten
  • array_forget
  • array_get
  • array_has
  • array_last
  • array_only
  • array_pluck
  • array_pull
  • array_replace_value
  • array_set
  • array_sort
  • array_sort_recursive
  • array_where
  • cache
  • camel_case
  • class_basename
  • class_uses_recursive
  • collect
  • container
  • data_get
  • dd
  • def
  • dump
  • e
  • ends_with
  • fluent
  • get
  • head
  • input
  • last
  • object_get
  • preg_replace_sub
  • request
  • response
  • session
  • snake_case
  • starts_with
  • str_contains
  • str_finish
  • str_is
  • str_limit
  • str_random
  • str_replace_array
  • str_slug
  • studly_case
  • title_case
  • trait_uses_recursive
  • transaction
  • trimtolower
  • value
  • with
  1 <?php
  2 
  3 namespace Docolight\Rest\Http;
  4 
  5 use Yii;
  6 use Exception;
  7 use CController;
  8 use Docolight\Http\Response;
  9 
 10 /**
 11  * A restful controller.
 12  *
 13  * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
 14  */
 15 abstract class RestFulController extends CController
 16 {
 17     /**
 18      * Data stored in this implementation. This data will be rendered as json response later in `afterAction` method.
 19      *
 20      * @var array
 21      */
 22     protected $data;
 23 
 24     /**
 25      * Exception stored in this implementation. When exception raised, the controller stored the exception in it.
 26      *
 27      * @var \Exception
 28      */
 29     protected $error;
 30 
 31     /**
 32      * Headers to be sent in HTTP response.
 33      *
 34      * @var array
 35      */
 36     protected $headers = [
 37         'Powered-By' => 'PT. Docotel Teknologi',
 38     ];
 39 
 40     /**
 41      * Handling 404 error.
 42      *
 43      * @param string $action
 44      */
 45     public function missingAction($action)
 46     {
 47         return response('json', 404, fluent([
 48             'status' => 404,
 49             'message' => 'Not Found',
 50             'value' => "Request to [".Yii::app()->request->getPathInfo()."] has no resource.",
 51         ]), $this->headers)->send();
 52     }
 53 
 54     /**
 55      * Set an exception publicly.
 56      *
 57      * @param \Exception $error
 58      */
 59     public function setError($error)
 60     {
 61         // Remove the data
 62         $this->data = null;
 63 
 64         // Set new error
 65         $this->error = $error;
 66     }
 67 
 68     /**
 69      * After action.
 70      *
 71      * @param \CInlineAction $action Action from controller
 72      *
 73      * @return \Docolight\Http\Response
 74      */
 75     public function afterAction($action)
 76     {
 77         parent::afterAction($action);
 78 
 79         // Basic data template
 80         $statusCode = 200;
 81         $data = array(
 82             'status' => $statusCode,
 83             'message' => 'Success',
 84             'value' => $this->data,
 85         );
 86 
 87         // Let's find an error
 88         if ($this->error instanceof Exception) {
 89             // throw $this->error;
 90 
 91             // Basic data template for an exception
 92             $statusCode = 500;
 93             $data = [
 94                 'status' => $statusCode,
 95                 'message' => 'Error',
 96                 'value' => [
 97                     'code' => $this->error->getCode(),
 98                     'message' => $this->error->getMessage(),
 99                 ],
100             ];
101 
102             // If exception code is an HTTP resoponse code
103             if ($message = Response::getMessageForCode($this->error->getCode())) {
104                 $statusCode = $this->error->getCode();
105                 $data['status'] = $statusCode;
106                 $data['message'] = preg_replace('/^\d+ /', '', $message);
107             }
108             // If not, this is a system failure
109             // Trace the error if YII_DEBUG is defined
110             else {
111                 if (YII_DEBUG) {
112                     $data['value']['stack_trace'] = $this->error->getTrace();
113                 }
114             }
115         }
116         // If the data is a Reponse instance, send the response
117         elseif ($this->data instanceof Response) {
118             return $this->data->send();
119         }
120 
121         return response('json', $statusCode, collect($data), $this->headers)->send();
122     }
123 }
124 
LFT API documentation generated by ApiGen