Last active
November 26, 2021 10:49
-
-
Save chadrien/a201fba2f3de9ee2c0c9 to your computer and use it in GitHub Desktop.
CakePHP force trailing slash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('TrailingSlashUtil', 'Lib'); | |
App::uses('Controller', 'Controller'); | |
class AppCpontroller extends Controller { | |
public function redirect ($url, $status = null, $exit = true) { | |
$url = TrailingSlashRouter::url($url); | |
return parent::redirect($url, $status, $exit); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('TrailingSlashUtil', 'Lib'); | |
App::uses('Helper', 'Helper'); | |
class AppHelper extends Helper { | |
public function url ($url = null, $full = false) { | |
$url = TrailingSlashRouter::url($url); | |
return parent::url($url, $full); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Router', 'Routing'); | |
class TrailingSlashUtil{ | |
public static function url($url = null, $full = null) { | |
$matches = array(); | |
preg_match('`^(.*)((?:[^/]*\..*)?(?:\?.*)?(?:#.*)?)$`U', Router::url($url), $matches); | |
list(, $url, $queryString) = $matches; | |
if (substr($url, -1) !== '/') { | |
$url .= '/'; | |
} | |
return $url . $queryString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment