public function before() { parent::before(); //get header Accept $accep_types = Request::accept_type(); //replace any other media type unset($accep_types['*/*']); // throw exception if none of the accept-types are supported if (!(bool) ($response_format = array_intersect_key($this->_accept_formats, $accep_types))) { throw new HTTP_Exception_415('Unsupported Accept media type'); } else { //set current accept as response format $this->_response_format = key($response_format); //set response header $this->_headers['Content-type'] = $this->_response_format; } // throw exception if none version given // if(!isset($_SERVER['HTTP_API_VERSION'])){ // throw new HTTP_Exception_404('No version specified for this request'); // } }
/** * Tests Request::accept_type() * * @test * @covers Request::accept_type */ public function test_accept_type() { $this->assertEquals(array('*/*' => 1), Request::accept_type()); }
/** * Construct controller. */ public function before() { if ($this->request->is_ajax()) { $this->_request_type = self::REQUEST_AJAX; $this->ajax = true; } else { if (!$this->request->is_initial()) { $this->_request_type = self::REQUEST_INTERNAL; $this->internal = true; } } // Update history? $this->history = $this->history && !$this->ajax; // Initialize session $this->session = Session::instance(); // Load current user, null if none if (self::$user === false) { $visitor = Visitor::instance(); Controller::$user = $visitor->get_user(); // If still no user, try auto login if (!Controller::$user && $visitor->auto_login()) { Controller::$user = $visitor->get_user(); } unset($visitor); } // Update current online user for initial and ajax requests if ($this->_request_type !== self::REQUEST_INTERNAL) { Model_User_Online::update(self::$user); } // Open outside links to a new tab/window HTML::$windowed_urls = true; // Load template if ($this->auto_render) { $this->page_id = $this->page_id ? $this->page_id : $this->request->controller(); // Figure out what format the client wants $accept_types = Request::accept_type(); if (isset($accept_types['*/*'])) { // All formats accepted $accept_types = $this->_accept_formats; } else { // Only some formats accepted $accept_types = Arr::extract($accept_types, array_keys($this->_accept_formats)); if (!($accept_types = array_filter($accept_types))) { throw new HTTP_Exception_415('Unsupported accept type'); } } $this->_response_format = key($accept_types); } }
/** * Gets the content type based on what's accepted * @return string */ private function _get_contenttype() { if ($this->is_xhtml and Request::accept_type('application/xhtml+xml') > 0) { return 'application/xhtml+xml'; } return 'text/html'; }
/** * Loads the template View object, if it is direct request * * @return void * @throws Http_Exception_415 If none of the accept-types are supported */ public function before() { // Execute parent::before first parent::before(); if ($this->bare == FALSE) { // Load the config $this->_config = Config::load('site'); if (Kohana::$profiling) { // Start a new benchmark token $this->_benchmark = Profiler::start('Gleez', ucfirst($this->request->controller()) . ' Controller'); } // Test whether the current request is command line request if (Kohana::$is_cli) { $this->_ajax = FALSE; $this->auto_render = FALSE; } // Test whether the current request is the first request if (!$this->request->is_initial()) { $this->_internal = TRUE; $this->auto_render = FALSE; } // Test whether the current request is ajax request if ($this->request->is_ajax()) { $this->_ajax = TRUE; $this->auto_render = FALSE; } // Test whether the current request is jquery mobile request. ugly hack if (Request::is_mobile() and $this->_config->get('mobile_theme', FALSE)) { $this->_ajax = FALSE; $this->auto_render = TRUE; } // Test whether the current request is datatables request if (Request::is_datatables()) { $this->_ajax = TRUE; $this->auto_render = FALSE; } $this->response->headers('X-Powered-By', Gleez::getVersion(TRUE, TRUE) . ' (' . Gleez::CODENAME . ')'); $this->_auth = Auth::instance(); // Get desired response formats $accept_types = Request::accept_type(); $accept_types = Arr::extract($accept_types, array_keys($this->_accept_formats)); // Set response format to first matched element $this->_response_format = $this->request->headers()->preferred_accept(array_keys($this->_accept_formats)); $site_name = Template::getSiteName(); $url = URL::site(NULL, TRUE); View::bind_global('site_name', $site_name); View::bind_global('site_url', $url); } if ($this->auto_render && $this->bare == FALSE) { // Throw exception if none of the accept-types are supported if (!($accept_types = array_filter($accept_types))) { throw new Http_Exception_415('Unsupported accept-type', 415); } // Initiate a Format instance $this->_format = Format::instance(); // Load the template $this->template = View::factory($this->template); $this->title_separator = $this->_config->get('title_separator', ' | '); $this->_widgets = Widgets::instance(); $this->template->_admin = Theme::$is_admin; // Set the destination & redirect url $this->_desti = array('destination' => $this->request->uri()); $this->redirect = $this->request->query('destination') !== NULL ? $this->request->query('destination') : array(); // Bind the generic page variables $this->template->set('site_name', Template::getSiteName())->set('site_slogan', $this->_config->get('site_slogan', __('Innovate IT')))->set('site_url', URL::site(NULL, TRUE))->set('site_logo', $this->_config->get('site_logo', FALSE))->set('sidebar_left', array())->set('sidebar_right', array())->set('column_class', '')->set('main_column', 12)->set('head_title', $this->title)->set('title', $this->title)->set('subtitle', $this->subtitle)->set('icon', $this->icon)->set('schemaType', $this->schemaType)->set('front', FALSE)->set('mission', FALSE)->set('tabs', FALSE)->set('subtabs', FALSE)->set('actions', FALSE)->set('_user', $this->_auth->get_user())->bind('datatables', $this->_datatables); // Page Title $this->title = ucwords($this->request->controller()); // Assign the default css files $this->_set_default_css(); // Assign the default js files $this->_set_default_js(); // Set default server headers $this->_set_default_server_headers(); // Set default meta data and media $this->_set_default_meta_links(); $this->_set_default_meta_tags(); /** * Make your view template available to all your other views * so easily you could access template variables */ View::bind_global('template', $this->template); } if (Kohana::$environment === Kohana::DEVELOPMENT) { Log::debug('Executing Controller [:controller] action [:action]', array(':controller' => $this->request->controller(), ':action' => $this->request->action())); } }