Bluz\Controller\Controller::checkAccept PHP Method

checkAccept() public method

Check Accept
public checkAccept ( )
    public function checkAccept()
    {
        // all ok for CLI
        if (PHP_SAPI == 'cli') {
            return;
        }
        $allowAccept = $this->getReflection()->getAccept();
        // some controllers hasn't @accept tag
        if (!$allowAccept) {
            // but by default allow just HTML output
            $allowAccept = [Request::TYPE_HTML, Request::TYPE_ANY];
        }
        // get Accept with high priority
        $accept = Request::getAccept($allowAccept);
        // some controllers allow any type (*/*)
        // and client doesn't send Accept header
        if (in_array(Request::TYPE_ANY, $allowAccept) && !$accept) {
            // all OK, controller should realize logic for response
            return;
        }
        // some controllers allow just selected types
        // choose MIME type by browser accept header
        // filtered by controller @accept
        // switch statement for this logic
        switch ($accept) {
            case Request::TYPE_ANY:
            case Request::TYPE_HTML:
                // HTML response with layout
                break;
            case Request::TYPE_JSON:
                // JSON response
                $this->template = null;
                break;
            default:
                throw new NotAcceptableException();
        }
    }

Usage Example

Example #1
0
 /**
  * Do dispatch
  *
  * @param  string $module
  * @param  string $controller
  * @param  array  $params
  * @return Controller
  */
 protected function doDispatch($module, $controller, $params = [])
 {
     // @TODO: try to find custom controller class
     // create controller controller
     $controllerInstance = new Controller($module, $controller);
     // check HTTP Accept header
     $controllerInstance->checkAccept();
     // check HTTP method
     $controllerInstance->checkMethod();
     // check ACL privileges
     $controllerInstance->checkPrivilege();
     // run controller
     $controllerInstance->run($params);
     return $controllerInstance;
 }