/** * Set up the basic uri filters in our static init and * also add a default 404 response * * @return void */ public static function _init() { // default string static::filter('any', '[a-zA-Z0-9' . ClanCats::$config->get('router.allowed_special_chars') . ']'); // only numbers static::filter('num', '[0-9]'); // only alpha characters static::filter('alpha', '[a-zA-Z]'); // only alphanumeric characters static::filter('alphanum', '[a-zA-Z0-9]'); // 404 not found error CCRouter::on('#404', function () { return CCResponse::create(CCView::create('Core::CCF/404')->render(), 404); }); }
/** * Sign out action */ public function action_fortunes() { $view = CCView::create('bench/fortune'); $fortunes = DB::select('Fortune')->run(); $runtimeFortune = new stdClass(); $runtimeFortune->id = 0; $runtimeFortune->message = 'Additional fortune added at request time.'; $fortunes[] = $runtimeFortune; usort($fortunes, function ($left, $right) { if ($left->message === $right->message) { return 0; } else { if ($left->message > $right->message) { return 1; } else { return -1; } } }); $view->fortunes = $fortunes; return CCResponse::create($view->render()); }
/** * Create the response object from an string * This function is needed for some subclasses * just like the view controller * * @param string * @return CCResponse */ protected function _respond($string) { return CCResponse::create($string, $this->status); }
/** * Execute the Request * * @param array $action * @param array $params * * @return self */ public function perform() { // set the input if (!is_null($this->input)) { CCIn::instance($this->input); } else { CCIn::instance(CCServer::instance()); } // set current request static::$_current =& $this; // route is invalid show 404 if (!$this->route instanceof CCRoute) { $this->route = CCRouter::resolve('#404'); } /* * call wake events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('wake', $this->route->uri) as $callback) { if (($return = CCContainer::call($callback)) instanceof CCResponse) { $this->response = $return; return $this; } } /* * a closure */ if (!is_array($this->route->callback) && is_callable($this->route->callback)) { // execute and capture the output ob_start(); // run the closure $return = call_user_func_array($this->route->callback, $this->route->params); // catch the output $output = ob_get_clean(); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the captured output $return = CCResponse::create($output); } } elseif (is_callable($this->route->callback)) { // execute the callback and get the return $return = call_user_func_array($this->route->callback, array($this->route->action, $this->route->params)); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the return as string $return = CCResponse::create((string) $return); } } else { $return = CCResponse::error(404); } // set the response $this->response = $return; /* * call sleep events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('sleep', $this->route->uri) as $callback) { if ($return = CCContainer::call($callback, $this->response) instanceof CCResponse) { $this->response = $return; return $this; } } return $this; }
/** * return a response */ public function action_response() { return \CCResponse::create('Response'); }
/** * Test response send */ public function testSend() { // create an instance $response = CCResponse::create(); // just run the method to check for errors $response->send(); // just run the method to check for errors $response->send(true); }
/** * Server error 500 */ public function action_500() { return CCResponse::create(CCView::create('Core::CCF/500')->render(), 500); }
/** * Create a CCRespone of the image * * @param string $quality The image quality * @param string $type jpg|png|gif * @return CCresponse */ public function response($quality = null, $type = null) { $response = CCResponse::create($this->stringify($quality, $type)); if (!is_null($this->type)) { $response->header('Content-Type', 'image/' . $this->type); } return $response; }
/** * action with param */ public function action_param($p1) { return \CCResponse::create('Test ' . $p1); }
/** * Returns an test CCResposne */ public function test_response() { return CCResponse::create('Callbacks are pretty cool'); }
} if ($queries > 500) { $queries = 500; } $worlds = array(); for ($i = 0; $i < $queries; ++$i) { $world = DB::select('World')->find(mt_rand(1, 10000)); $world->id = intval($world->id); $world->randomNumber = intval($world->randomNumber); $worlds[] = $world; } return CCResponse::create(json_encode($worlds), 200)->header('Content-Type', 'application/json'); }, 'updates' => function () { $queries = CCIn::get('queries', 1); if ($queries < 1) { $queries = 1; } if ($queries > 500) { $queries = 500; } $worlds = array(); for ($i = 0; $i < $queries; ++$i) { $id = mt_rand(1, 10000); DB::update('World')->set('randomNumber', mt_rand(1, 10000))->where('id', $id)->run(); $world = DB::select('World')->find($id); $world->id = intval($world->id); $world->randomNumber = intval($world->randomNumber); $worlds[] = $world; } return CCResponse::create(json_encode($worlds), 200)->header('Content-Type', 'application/json'); }, 'fortunes' => 'Bench@fortunes');