Skip to content

Commit

Permalink
Add warnAboutOldRouting shimming
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Dec 12, 2015
1 parent 0b8118d commit b60c5e3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
1 change: 1 addition & 0 deletions Config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
//Configure::write('Shim.deprecateHasAny', true);
//Configure::write('Shim.deprecateField', true);
//Configure::write('Shim.deprecateSaveField', true);
//Configure::write('Shim.warnAboutOldRouting', true);
32 changes: 32 additions & 0 deletions View/Helper/HtmlShimHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ public function link($title, $url = null, $options = [], $confirmMessage = false
return parent::link($title, $url, $options, $confirmMessage);
}

/**
* Returns a URL based on provided parameters.
*
* @param string|array|null $url Either a relative string url like `/products/view/23` or
* an array of URL parameters. Using an array for URLs will allow you to leverage
* the reverse routing features of CakePHP.
* @param bool $full If true, the full base URL will be prepended to the result
* @return string Full translated URL with base path.
*/
public function url($url = null, $full = false) {
if (is_array($url)) {
if (Configure::read('Shim.warnAboutOldRouting')) {
if (isset($url['ext'])) {
trigger_error('Param `ext` should be `_ext` in URL arrays.', E_USER_DEPRECATED);
}
if (isset($url['full_base'])) {
trigger_error('Param `full_base` should be `_full` in URL arrays.', E_USER_DEPRECATED);
}
}
if (isset($url['_ext'])) {
$url['ext'] = $url['_ext'];
unset($url['_ext']);
}
if (isset($url['_full'])) {
$url['full_base'] = $url['_full'];
unset($url['_full']);
}
}

return parent::url($url, $full);
}

/**
* Creates a link element for CSS stylesheets.
*
Expand Down
34 changes: 33 additions & 1 deletion View/Helper/UrlShimHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,45 @@ class UrlShimHelper extends AppHelper {
/**
* Returns a URL based on provided parameters.
*
* @param string|array $url Either a relative string url like `/products/view/23` or
* @param string|array|null $url Either a relative string url like `/products/view/23` or
* an array of URL parameters. Using an array for URLs will allow you to leverage
* the reverse routing features of CakePHP.
* @param bool $full If true, the full base URL will be prepended to the result
* @return string Full translated URL with base path.
*/
public function build($url = null, $full = false) {
return $this->url($url, $full);
}

/**
* Returns a URL based on provided parameters.
*
* @param string|array|null $url Either a relative string url like `/products/view/23` or
* an array of URL parameters. Using an array for URLs will allow you to leverage
* the reverse routing features of CakePHP.
* @param bool $full If true, the full base URL will be prepended to the result
* @return string Full translated URL with base path.
*/
public function url($url = null, $full = false) {
if (is_array($url)) {
if (Configure::read('Shim.warnAboutOldRouting')) {
if (isset($url['ext'])) {
trigger_error('Param `ext` should be `_ext` in URL arrays.', E_USER_DEPRECATED);
}
if (isset($url['full_base'])) {
trigger_error('Param `full_base` should be `_full` in URL arrays.', E_USER_DEPRECATED);
}
}
if (isset($url['_ext'])) {
$url['ext'] = $url['_ext'];
unset($url['_ext']);
}
if (isset($url['_full'])) {
$url['full_base'] = $url['_full'];
unset($url['_full']);
}
}

return parent::url($url, $full);
}

Expand Down
11 changes: 7 additions & 4 deletions docs/Routing.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
## Routing

### Deprecate old routing
With Configure `Shim.warnAboutOldRouting` you can make the URL arrays 3.x compatible already and warn about old usages.

### Routing Speedup
With the `Config/routes.speedup.php` file content you can speed up your 2.6+ routing.
It will pre-compile and cache your routes for quicker re-use on each page load for you.

See the explanations inside this file on how to use.

### Limitations
#### Limitations
There is some limitations on this approach:
- If you have any conditional check it will not work. The resultant is the routes with the conditional matching for the time where the cache was written.
- If you generate routes dynamically (ie, from database) it also cause problems. The routes are going to be generated at the time you create the cache,
- If you generate routes dynamically (ie, from database) it also cause problems. The routes are going to be generated at the time you create the cache,
but if you add new routes on database it will not be available.
- If you have dynamic load for plugins, plugins update, etc it can be a problem as well because the cache is based on the sha1 of the routes.php on the app.
- When running through console, there might also be some tricky things to be done.

You might be able to still leverage it by using different cache keys or otherwise slightly modifying the script, though.

### Real life examples
#### Real life examples
Performance analysis on some real life projects to come soon as benchmark..

### Credits
#### Credits
Credits to [jrbasso](https://github.com/jrbasso) for the ground works on this little snippet.

0 comments on commit b60c5e3

Please sign in to comment.