/** * @return array|bool */ public function matchCurrentRoute() { $context = new RequestContext(); $matcher = new UrlMatcher($this->routes, $context); $request = new CakeRequest(); try { return $matcher->match($request->here()); } catch (Exception $e) { //route is not registered in yml file return false; } }
/** * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set * to autoRender, via Controller::$autoRender, then Dispatcher will render the view. * * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you * want controller methods to be public and in-accessible by URL, then prefix them with a `_`. * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods * are also not accessible via URL. * * If no controller of given name can be found, invoke() will throw an exception. * If the controller is found, and the action is not found an exception will be thrown. * * @param CakeRequest $request Request object to dispatch. * @param CakeResponse $response Response object to put the results of the dispatch into. * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params * @return boolean Success * @throws MissingControllerException When the controller is missing. */ public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) { if ($this->asset($request->url, $response) || $this->cached($request->here())) { return; } Router::setRequestInfo($request); $request = $this->parseParams($request, $additionalParams); $controller = $this->_getController($request, $response); if (!$controller instanceof Controller) { throw new MissingControllerException(array('class' => Inflector::camelize($request->params['controller']) . 'Controller', 'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin']))); } return $this->_invoke($controller, $request, $response); }
/** * Validate submitted form * * @param Controller $controller Instantiating controller * @return bool true if submitted form is valid */ protected function _validatePost(Controller $controller) { if (empty($controller->request->data)) { return true; } $data = $controller->request->data; if (!isset($data['_Token']) || !isset($data['_Token']['fields']) || !isset($data['_Token']['unlocked'])) { return false; } $locked = ''; $check = $controller->request->data; $token = urldecode($check['_Token']['fields']); $unlocked = urldecode($check['_Token']['unlocked']); if (strpos($token, ':')) { list($token, $locked) = explode(':', $token, 2); } unset($check['_Token']); $locked = explode('|', $locked); $unlocked = explode('|', $unlocked); $lockedFields = array(); $fields = Hash::flatten($check); $fieldList = array_keys($fields); $multi = array(); foreach ($fieldList as $i => $key) { if (preg_match('/(\\.\\d+){1,10}$/', $key)) { $multi[$i] = preg_replace('/(\\.\\d+){1,10}$/', '', $key); unset($fieldList[$i]); } } if (!empty($multi)) { $fieldList += array_unique($multi); } $unlockedFields = array_unique(array_merge((array) $this->disabledFields, (array) $this->unlockedFields, $unlocked)); foreach ($fieldList as $i => $key) { $isLocked = is_array($locked) && in_array($key, $locked); if (!empty($unlockedFields)) { foreach ($unlockedFields as $off) { $off = explode('.', $off); $field = array_values(array_intersect(explode('.', $key), $off)); $isUnlocked = $field === $off; if ($isUnlocked) { break; } } } if ($isUnlocked || $isLocked) { unset($fieldList[$i]); if ($isLocked) { $lockedFields[$key] = $fields[$key]; } } } sort($unlocked, SORT_STRING); sort($fieldList, SORT_STRING); ksort($lockedFields, SORT_STRING); $fieldList += $lockedFields; $unlocked = implode('|', $unlocked); $hashParts = array($this->request->here(), serialize($fieldList), $unlocked, Configure::read('Security.salt')); $check = Security::hash(implode('', $hashParts), 'sha1'); return $token === $check; }
/** * Test the here() with space in URL * * @return void */ public function testHereWithSpaceInUrl() { Configure::write('App.base', ''); $_GET = array('/admin/settings/settings/prefix/Access_Control' => ''); $request = new CakeRequest('/admin/settings/settings/prefix/Access%20Control'); $result = $request->here(); $this->assertEquals('/admin/settings/settings/prefix/Access%20Control', $result); }
/** * リクエストをリダイレクトするURLを生成 * * @param CakeRequest $request リクエスト * @return string */ public function makeRedirectUrl(CakeRequest $request) { $hereWithQuery = $request->here(false); $alias = static::extractAlias($request->url); if (is_null($alias)) { return "{$this->alias}{$hereWithQuery}"; } $replacedUrl = preg_replace('/^\\/' . $alias . '/', $this->alias, $hereWithQuery); return $replacedUrl; }
/** * test the here() method * * @return void */ public function testHere() { Configure::write('App.base', '/base_path'); $_GET = array('test' => 'value'); $request = new CakeRequest('/posts/add/1/name:value'); $result = $request->here(); $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result); $result = $request->here(false); $this->assertEquals('/posts/add/1/name:value?test=value', $result); $request = new CakeRequest('/posts/base_path/1/name:value'); $result = $request->here(); $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result); $result = $request->here(false); $this->assertEquals('/posts/base_path/1/name:value?test=value', $result); }
/** * エイリアスを反映したURLを生成 * 同一URL設定のみ利用可 * * @param CakeRequest $request リクエスト * @return string */ public function makeUrl(CakeRequest $request) { $here = $request->here(false); if ($this->alias) { return "/{$this->alias}{$here}"; } else { return $here; } }