本文整理汇总了PHP中Cake\Routing\Router::reverse方法的典型用法代码示例。如果您正苦于以下问题:PHP Router::reverse方法的具体用法?PHP Router::reverse怎么用?PHP Router::reverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Routing\Router
的用法示例。
在下文中一共展示了Router::reverse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parse
/**
* Parses a string URL into an array. Parsed URLs will result in an automatic
* redirection.
*
* @param string $url The URL to parse.
* @return false|null False on failure. An exception is raised on a successful match.
* @throws \Cake\Routing\Exception\RedirectException An exception is raised on successful match.
* This is used to halt route matching and signal to the middleware that a redirect should happen.
*/
public function parse($url)
{
$params = parent::parse($url);
if (!$params) {
return false;
}
$redirect = $this->redirect;
if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
$redirect = $this->redirect[0];
}
if (isset($this->options['persist']) && is_array($redirect)) {
$redirect += ['pass' => $params['pass'], 'url' => []];
if (is_array($this->options['persist'])) {
foreach ($this->options['persist'] as $elem) {
if (isset($params[$elem])) {
$redirect[$elem] = $params[$elem];
}
}
}
$redirect = Router::reverse($redirect);
}
$status = 301;
if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
$status = $this->options['status'];
}
throw new RedirectException(Router::url($redirect, true), $status);
}
示例2: testReverseWithExtension
/**
* Test that extensions work with Router::reverse()
*
* @return void
*/
public function testReverseWithExtension()
{
Router::connect('/:controller/:action/*');
Router::parseExtensions('json', false);
$request = new Request('/posts/view/1.json');
$request->addParams(array('controller' => 'posts', 'action' => 'view', 'pass' => array(1), 'ext' => 'json'));
$request->query = [];
$result = Router::reverse($request);
$expected = '/posts/view/1.json';
$this->assertEquals($expected, $result);
}
示例3: requestAction
/**
* Calls a controller's method from any location. Can be used to connect controllers together
* or tie plugins into a main application. requestAction can be used to return rendered views
* or fetch the return value from controller actions.
*
* Under the hood this method uses Router::reverse() to convert the $url parameter into a string
* URL. You should use URL formats that are compatible with Router::reverse()
*
* ### Examples
*
* A basic example getting the return value of the controller action:
*
* ```
* $variables = $this->requestAction('/articles/popular');
* ```
*
* A basic example of request action to fetch a rendered page without the layout.
*
* ```
* $viewHtml = $this->requestAction('/articles/popular', ['return']);
* ```
*
* You can also pass the URL as an array:
*
* ```
* $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
* ```
*
* ### Passing other request data
*
* You can pass POST, GET, COOKIE and other data into the request using the appropriate keys.
* Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
* data can be sent using the `post` key.
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'query' => ['page' => 1],
* 'cookies' => ['remember_me' => 1],
* ]);
* ```
*
* ### Sending environment or header values
*
* By default actions dispatched with this method will use the global $_SERVER and $_ENV
* values. If you want to override those values for a request action, you can specify the values:
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'environment' => ['CONTENT_TYPE' => 'application/json']
* ]);
* ```
*
* ### Transmitting the session
*
* By default actions dispatched with this method will use the standard session object.
* If you want a particular session instance to be used, you need to specify it.
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'session' => new Session($someSessionConfig)
* ]);
* ```
*
* @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
* url will not automatically handle passed arguments in the $url parameter.
* @param array $extra if array includes the key "return" it sets the autoRender to true. Can
* also be used to submit GET/POST data, and passed arguments.
* @return mixed Boolean true or false on success/failure, or contents
* of rendered action if 'return' is set in $extra.
* @deprecated 3.3.0 You should refactor your code to use View Cells instead of this method.
*/
public function requestAction($url, array $extra = [])
{
if (empty($url)) {
return false;
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra += ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1];
$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
$url = Router::normalize(str_replace($baseUrl, '', $url));
}
if (is_string($url)) {
$params = ['url' => $url];
} elseif (is_array($url)) {
$defaultParams = ['plugin' => null, 'controller' => null, 'action' => null];
$params = ['params' => $url + $defaultParams, 'base' => false, 'url' => Router::reverse($url)];
if (empty($params['params']['pass'])) {
$params['params']['pass'] = [];
}
}
$current = Router::getRequest();
if ($current) {
$params['base'] = $current->base;
$params['webroot'] = $current->webroot;
}
//.........这里部分代码省略.........
示例4: requestAction
/**
* Calls a controller's method from any location. Can be used to connect controllers together
* or tie plugins into a main application. requestAction can be used to return rendered views
* or fetch the return value from controller actions.
*
* Under the hood this method uses Router::reverse() to convert the $url parameter into a string
* URL. You should use URL formats that are compatible with Router::reverse()
*
* ### Examples
*
* A basic example getting the return value of the controller action:
*
* ```
* $variables = $this->requestAction('/articles/popular');
* ```
*
* A basic example of request action to fetch a rendered page without the layout.
*
* ```
* $viewHtml = $this->requestAction('/articles/popular', ['return']);
* ```
*
* You can also pass the URL as an array:
*
* ```
* $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
* ```
*
* ### Passing other request data
*
* You can pass POST, GET, COOKIE and other data into the request using the appropriate keys.
* Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
* data can be sent using the `post` key.
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'query' => ['page' => 1],
* 'cookies' => ['remember_me' => 1],
* ]);
* ```
*
* ### Sending environment or header values
*
* By default actions dispatched with this method will use the global $_SERVER and $_ENV
* values. If you want to override those values for a request action, you can specify the values:
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'environment' => ['CONTENT_TYPE' => 'application/json']
* ]);
* ```
*
* ### Transmitting the session
*
* By default actions dispatched with this method will use the standard session object.
* If you want a particular session instance to be used, you need to specify it.
*
* ```
* $vars = $this->requestAction('/articles/popular', [
* 'session' => new Session($someSessionConfig)
* ]);
* ```
*
* @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
* url will not automatically handle passed arguments in the $url parameter.
* @param array $extra if array includes the key "return" it sets the autoRender to true. Can
* also be used to submit GET/POST data, and passed arguments.
* @return mixed Boolean true or false on success/failure, or contents
* of rendered action if 'return' is set in $extra.
*/
public function requestAction($url, array $extra = [])
{
if (empty($url)) {
return false;
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra += ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1];
$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
$url = Router::normalize(str_replace($baseUrl, '', $url));
}
if (is_string($url)) {
$params = ['url' => $url];
} elseif (is_array($url)) {
$defaultParams = ['plugin' => null, 'controller' => null, 'action' => null];
$params = ['params' => $url + $defaultParams, 'base' => false, 'url' => Router::reverse($url)];
if (empty($params['params']['pass'])) {
$params['params']['pass'] = [];
}
}
$current = Router::getRequest();
if ($current) {
$params['base'] = $current->base;
$params['webroot'] = $current->webroot;
}
$params['post'] = $params['query'] = [];
//.........这里部分代码省略.........
示例5: isUrlAuthorized
/**
* Check if a given url is authorized
*
* @param Event $event event
*
* @return bool
*/
public function isUrlAuthorized(Event $event)
{
$url = Hash::get((array) $event->data, 'url');
if (empty($url)) {
return false;
}
if (is_array($url)) {
$requestUrl = Router::reverse($url);
$requestParams = Router::parse($requestUrl);
} else {
try {
//remove base from $url if exists
$normalizedUrl = Router::normalize($url);
$requestParams = Router::parse($normalizedUrl);
} catch (MissingRouteException $ex) {
//if it's a url pointing to our own app
if (substr($normalizedUrl, 0, 1) === '/') {
throw $ex;
}
return true;
}
$requestUrl = $url;
}
// check if controller action is allowed
if ($this->_isActionAllowed($requestParams)) {
return true;
}
// check we are logged in
$user = $this->_registry->getController()->Auth->user();
if (empty($user)) {
return false;
}
$request = new Request($requestUrl);
$request->params = $requestParams;
$isAuthorized = $this->_registry->getController()->Auth->isAuthorized(null, $request);
return $isAuthorized;
}
示例6: isUrlAuthorized
/**
* Check if a given url is authorized
*
* @param Event $event event
*
* @return bool
*/
public function isUrlAuthorized(Event $event)
{
$user = $this->_registry->getController()->Auth->user();
if (empty($user)) {
return false;
}
$url = Hash::get((array) $event->data, 'url');
if (empty($url)) {
return false;
}
if (is_array($url)) {
$requestUrl = Router::reverse($url);
$requestParams = Router::parse($requestUrl);
} else {
$requestParams = Router::parse($url);
$requestUrl = $url;
}
$request = new Request($requestUrl);
$request->params = $requestParams;
$isAuthorized = $this->_registry->getController()->Auth->isAuthorized(null, $request);
return $isAuthorized;
}
示例7: requestAction
/**
* Calls a controller's method from any location. Can be used to connect controllers together
* or tie plugins into a main application. requestAction can be used to return rendered views
* or fetch the return value from controller actions.
*
* Under the hood this method uses Router::reverse() to convert the $url parameter into a string
* URL. You should use URL formats that are compatible with Router::reverse()
*
* ### Examples
*
* A basic example getting the return value of the controller action:
*
* {{{
* $variables = $this->requestAction('/articles/popular');
* }}}
*
* A basic example of request action to fetch a rendered page without the layout.
*
* {{{
* $viewHtml = $this->requestAction('/articles/popular', ['return']);
* }}}
*
* You can also pass the URL as an array:
*
* {{{
* $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
* }}}
*
* ### Passing other request data
*
* You can pass POST, GET, COOKIE and other data into the request using the apporpriate keys.
* Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
* data can be sent using the `post` key.
*
* {{{
* $vars = $this->requestAction('/articles/popular', [
* 'query' => ['page' = > 1],
* 'cookies' => ['remember_me' => 1],
* ]);
* }}}
*
* @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
* url will not automatically handle passed arguments in the $url parameter.
* @param array $extra if array includes the key "return" it sets the autoRender to true. Can
* also be used to submit GET/POST data, and passed arguments.
* @return mixed Boolean true or false on success/failure, or contents
* of rendered action if 'return' is set in $extra.
*/
public function requestAction($url, array $extra = array())
{
if (empty($url)) {
return false;
}
if (($index = array_search('return', $extra)) !== false) {
$extra['return'] = 0;
$extra['autoRender'] = 1;
unset($extra[$index]);
}
$extra = array_merge(['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1], $extra);
$post = $query = [];
if (isset($extra['post'])) {
$post = $extra['post'];
}
if (isset($extra['query'])) {
$query = $extra['query'];
}
unset($extra['post'], $extra['query']);
if (is_string($url) && strpos($url, Configure::read('App.fullBaseUrl')) === 0) {
$url = Router::normalize(str_replace(Configure::read('App.fullBaseUrl'), '', $url));
}
if (is_string($url)) {
$params = ['url' => $url];
} elseif (is_array($url)) {
$params = array_merge($url, ['pass' => [], 'base' => false, 'url' => Router::reverse($url)]);
}
if (!empty($post)) {
$params['post'] = $post;
}
if (!empty($query)) {
$params['query'] = $query;
}
$request = new Request($params);
$dispatcher = new Dispatcher();
$result = $dispatcher->dispatch($request, new Response(), $extra);
Router::popRequest();
return $result;
}
示例8: parse
/**
* Parses a string URL into an array. Parsed URLs will result in an automatic
* redirection
*
* @param string $url The URL to parse
* @return bool False on failure
*/
public function parse($url)
{
$params = parent::parse($url);
if (!$params) {
return false;
}
if (!$this->response) {
$this->response = new Response();
}
$redirect = $this->redirect;
if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
$redirect = $this->redirect[0];
}
if (isset($this->options['persist']) && is_array($redirect)) {
$redirect += ['pass' => $params['pass'], 'url' => []];
if (is_array($this->options['persist'])) {
foreach ($this->options['persist'] as $elem) {
if (isset($params[$elem])) {
$redirect[$elem] = $params[$elem];
}
}
}
$redirect = Router::reverse($redirect);
}
$status = 301;
if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
$status = $this->options['status'];
}
$this->response->header(['Location' => Router::url($redirect, true)]);
$this->response->statusCode($status);
$this->response->send();
$this->response->stop();
}